日期:2014-05-17  浏览次数:20554 次

关于MsSQL存储过程语法的疑问
存储过程如下:
USE [Flowmaster]
GO
/****** Object:  StoredProcedure [dbo].[dt_adduserobject]    Script Date: 12/05/2013 11:40:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
** Add an object to the dtproperties table
*/
ALTER procedure [dbo].[dt_adduserobject]
as
set nocount on
/*
** Create the user object if it does not exist already
*/
[color=#FF0000]begin transaction
insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT')
update dbo.dtproperties set objectid=@@identity 
where id=@@identity and property='DtgSchemaOBJECT'
commit

return @@identity[/color]
求解释红色字体的几句的意思。dbo.dtproperties是什么意思?'DtgSchemaOBJECT'是什么意思?objectid,id,以及property又是什么呢?以及以上几者之间的关系。我是初学者,请讲的详细一些,谢谢!如果懒得敲给个链接我自己看也行,谢谢!

------解决方案--------------------
引用:
begin transaction
insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT')
update dbo.dtproperties set objectid=@@identity 
where id=@@identity and property='DtgSchemaOBJECT'
commit
return @@identity


其实这个就是一个插入,然后是更新操作。

首先,把这个值'DtgSchemaOBJECT' 插入到表dtproperties的 字段property 中。

然后,是update表dtproperties的objectid字段,你的dtproperties表,应该是包含了一个identity,也就是自增列,当上面插入了记录后,@@identity中就保存了,插入到你的自增列中的值,

于是,通过条件where id=@@identity and property='DtgSchemaOBJECT',来找到这条刚刚插入的记录,更新objectid为@@identity 的值,不过这个update语句完全可以写成:

update dbo.dtproperties set objectid=id 
where id=@@identity and property='DtgSchemaOBJECT'
------解决方案--------------------
引用:
存储过程如下:
USE [Flowmaster]
GO
/****** Object:  StoredProcedure [dbo].[dt_adduserobject]    Script Date: 12/05/2013 11:40:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
** Add an object to the dtproperties table
*/
ALTER procedure [dbo].[dt_adduserobject]
as
set nocount on
/*
** Create the user object if it does not exist already
*/
[color=#FF0000]begin transaction
insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT')
u