1、
--把一个表中的数据复制到另一个表中,into可以省略
insert into [User] select StudentId,Email,[Password],[Role] from [User]
2、
--创建临时表tempbel并把User表中数据放临时表中
select distinct * into #tempdel from [User]
/*******
假设你要处理的表名是: pludetail
可搜索以用以下过程来实现,速度不在下面过程的考虑之中
如何去除表中相同记录
首先查询不重复的记录插入到临时表中,然后清空原有的表中数据,然后把临时表中的数据插入回来
*********/
create procedure distinct_deal
as
begin
begin transaction
select distinct * into #tempdel from [User] --提取无重复的记录到临时表中
truncate table pludetail --清掉原表
insert [User] select * from #tempdel --把临时表中无重复的数据插回原表
drop table #tempdel
if @@error<>0
begin
raiserror('数据处理失败!',16,-1)
goto error_deal
end
commit transaction
return
error_deal:
rollback transaction
return
end
3、
--sql的goto语句
/*将执行流更改到标签处。 跳过 GOTO 后面的 Transact-SQL 语句,并从标签位置继续处理。 GOTO 语句和标签可在过程、批处理或语句块中的任何位置使用。 GOTO 语句可嵌套使用。
*/
DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
SELECT @Counter
SET @Counter = @Counter + 1
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
IF @Counter = 5 GOTO Branch_Two --This will never execute.
END
Branch_One:
SELECT 'Jumping To Branch One.'
GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
SELECT 'Jumping To Branch Two.'
Branch_Three:
SELECT 'Jumping To Branch Three.';
4、
--RAISERROR 函数
--语法
RAISERROR ( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
raiserror('数据处理失败!',16,-1)
/*
参数1:用户定义消息,该错误消息最长可以有 2,047 个字符。
参数2:用户定义的与该消息关联的严重级别。任何用户都可以指定 0 到 18 之间的严重级别。 只有 sysadmin 固定服务器角色成员或具有 ALTER TRACE 权限的用户才能指定 19 到 25 之间的严重级别。
可以指定“-1”来返回与以下示例中所示的错误关联的严重性值。
参数3:0 到 255 之间的整数。 负值或大于 255 的值将生成错误。 可以指定“-1”来返回与以下示例中所示的 severity 定义中的错误关联的值。
*/
/*联立其他表更新其中一个表的字段*/
update [User] set Pwd='1111' from UserInfo where [User].Guid=UserInfo.UserGuid
update [User] set Pwd=UserInfo.Sex from [User],UserInfo where [User].Guid=UserInfo.UserGuid
update [User] set Pwd=(select Sex from UserInfo where UserInfo.UserGuid =[User].Guid)