首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

数据库操作的一些基本有关问题

2013-01-01 
数据库操作的一些基本问题1、往数据库中插入一条记录时(insert into table values(column1,column2…))

数据库操作的一些基本问题
1、往数据库中插入一条记录时(insert into table values('column1','column2'…))怎样在插入空值的时候让其自动补充为默认值??(因为属性列较多,所以不想对单独的列进行添加数据库操作的一些基本有关问题,希望能够自动把插入的空值转换为默认值)希望各位前辈能够帮忙解决一下;
2、有一个表的主码是两个属性列组合而成的,我想用

为了这个问题愁死啦···数据库操作的一些基本有关问题
[解决办法]

--试试,不知道行不行
create table TableTestPrimayKey(
SNO varchar(50),
BOOKNO varchar(50),
BTIEM DATETIME,
REMARK VARCHAR(200)
)

INSERT INTO TableTestPrimayKey VALUES(NEWID(),NEWID(),GETDATE(),NEWID())
GO 100;

SELECT TOP 5 *
  FROM TableTestPrimayKey
 WHERE SNO NOT IN (SELECT A.SNO
                     FROM (SELECT TOP 10 SNO, BOOKNO
                             FROM TableTestPrimayKey
                            ORDER BY BTIEM) A
                   
                   )
   AND BOOKNO NOT IN (SELECT A.BOOKNO
                        FROM (SELECT TOP 10 SNO, BOOKNO
                                FROM TableTestPrimayKey
                               ORDER BY BTIEM) A)

[解决办法]
这是我做过的也许能够帮助你if exists(select* from sysobjects where name='one_')
drop trigger one_
go
create trigger one_
on class
for insert
as 
begin
declare @one int
declare @two int
select @one=classid from inserted 
select @two=studentid from student
insert into sc (classid,studentid) values(@one,@two)
end
[解决办法]
靠,误操作,没写完就提交了。
MSSQL2005及以上版本:

;with aaa as
(
    select row_number() over(order by col1,col2) as rowindex,* from table1
)
select top (n-m+1) rowindex from aaa where rowindex not in (select top m-1 from aaa)

这样试试
[解决办法]


第一个问题,按楼主说的意思,貌似必须用触发器,用默认值解决不了问题,因为楼主说的是插入空值,而不是未插入值.如果这个列在插入语句中存在,但插入的值是空值,那必须用触发器更改.
create trigger updatenull on tb
INSTEAD OF INSERT 
as
begin
--可适用于批量插入
--先插入不空的行
insert into tb select * from inserted where col2 is not null

--再插入为空时的行,设Col2为检测的列,默认值为'xx'
insert into tb select col1,'xx',col3,..... from inserted where col2 is null

end 
go

[解决办法]
第二个问题,如果是分页,那必须另产生一个序列号,用这个序列号来进行分页.可用row_number()函数,也可以用identity函数获得临时表再分页.
select ....此处用分页的设置 from(
select row_number()over(order by 学号,书号) as rn,* from ...
)t

热点排行
Bad Request.