--事务实现实例Create procedure prInsertProducts(@intProductId int,@chvProductName varchar(30),@intProductCount int)ASDeclare @intErrorCode intSelect @intErrorCode=@@ErrorBegin transactionif @intErrorCode=0begin--insert productsinsert products(ProductID,ProductName,ProductCount)values(@intProductId,@chvProductName,@intProductCount)Select @intErrorCode=@@Error --每执行完一条t-sql语句马上进行检测,并把错误号保存到局部变量中endif @intErrorCode=0begin--update productsupdate products set ProductName='MicroComputer' where ProductID=5Select @intErrorCode=@@Errorendif @intErrorCode=0commit transactionelserollback transactionReturn @intErrorCode --最好返回错误代号给调用的存储过程或应用程序use tempdbgodeclare @A table(id int, name nvarchar(20),type nvarchar(20))declare @B table(id int, fid int,value nvarchar(20))insert into @Aselect 1,'n1','t1'union allselect 2,'n2','t2'insert into @Bselect 1,1,'v1'union allselect 2,2,'v2'select a.name,a.type,b.valuefrom @A a left join @B bon a.id = b.fid and a.id =1union allselect a.name,a.type,b.valuefrom @A a left join @B bon a.id = b.fid and a.id = 2declare my cursor for select name from sysobjects where xtype='U'declare @ID nvarchar(50)open myfetch next from my into @IDwhile(@@FETCH_STATUS=0)beginselect top 10 * from @ID where 1=1fetch next from my into @IDendclose mydeallocate mysp_helptext Test_select---------------------------------------------------------一对多时,多行转一列-------------------------------------------------------------------------use tempdbGodeclare @Class table(ID int,ClassName nvarchar(10))declare @Student table(ID int,StuName nvarchar(10),ClassID int)insert into @Classselect 1,'数学'union all select 2,'语文'union all select 3,'英语'insert into @Studentselect 1,'张三',1union all select 2,'张三',2union all select 3,'李四',1union all select 4,'王五',2union all select 5,'王五',1select ',' + S.StuNamefrom @Student Swhere S.ClassID = 1for xml path('')select * from @Classselect * from @StudentSELECT C.ID, C.ClassName,stuff((select ',' + S.StuNamefrom @Student Swhere S.ClassID = C.IDfor xml path('')),1,1,'') as stuNameFROM @Class Cselect STUFF(',张三,李四,王五',1,1,'')Go
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。