Insert | stored procedure
I've found two stored procedures that automatically generate a stored procedure for inserting and updating a datasheet, now dedicated to everyone!
Insert:
Create procedure Sp_geninsert
@TableName varchar (130),
@ProcedureName varchar (130)
As
SET NOCOUNT ON
DECLARE @maxcol int,
@TableID int
Set @TableID = object_id (@TableName)
Select @MaxCol = max (Colorder)
From syscolumns
WHERE id = @TableID
Select ' Create Procedure ' + rtrim (@ProcedureName) as type,0 as Colorder into #TempProc
Union
Select CONVERT (char (), ' @ ' + syscolumns.name)
+ RTrim (systypes.name)
+ Case when RTrim (Systypes.name) in (' Binary ', ' char ', ' nchar ', ' nvarchar ', ' varbinary ', ' varchar ') Then ' (' + RTrim ') (convert (char (4), syscolumns.length)) + ')'
When RTrim (Systypes.name) isn't in (' binary ', ' char ', ' nchar ', ' nvarchar ', ' varbinary ', ' varchar ') Then '
End
+ Case When Colorder < @maxcol then ', '
When colorder = @maxcol Then '
End
As type,
Colorder
From syscolumns
Join systypes on syscolumns.xtype = Systypes.xtype
WHERE id = @TableID and systypes.name <> ' sysname '
Union
Select ' as ', @maxcol + 1 as Colorder
Union
Select ' INSERT into ' + @TableName, @maxcol + 2 as Colorder
Union
Select ' (', @maxcol + 3 as Colorder
Union
Select Syscolumns.name
+ Case When Colorder < @maxcol then ', '
When colorder = @maxcol Then '
End
As type,
Colorder + @maxcol + 3 as Colorder
From syscolumns
Join systypes on syscolumns.xtype = Systypes.xtype
WHERE id = @TableID and systypes.name <> ' sysname '
Union
Select ') ', (2 * @maxcol) + 4 as Colorder
Union
Select ' VALUES ', (2 * @maxcol) + 5 as Colorder
Union
Select ' (', (2 * @maxcol) + 6 as Colorder
Union
Select ' @ ' + syscolumns.name
+ Case When Colorder < @maxcol then ', '
When colorder = @maxcol Then '
End
As type,
Colorder + (2 * @maxcol + 6) as Colorder
From syscolumns
Join systypes on syscolumns.xtype = Systypes.xtype
WHERE id = @TableID and systypes.name <> ' sysname '
Union
Select ') ', (3 * @maxcol) + 7 as Colorder
ORDER BY Colorder
Select type from #tempproc order by Colorder
Update:
Create procedure Sp_genupdate
@TableName varchar (130),
@PrimaryKey varchar (130),
@ProcedureName varchar (130)
As
SET NOCOUNT ON
DECLARE @maxcol int,
@TableID int
Set @TableID = object_id (@TableName)
Select @MaxCol = max (Colorder)
From syscolumns
WHERE id = @TableID
Select ' Create Procedure ' + rtrim (@ProcedureName) as type,0 as Colorder into #TempProc
Union
Select CONVERT (char (), ' @ ' + syscolumns.name)
+ RTrim (systypes.name)
+ Case when RTrim (Systypes.name) in (' Binary ', ' char ', ' nchar ', ' nvarchar ', ' varbinary ', ' varchar ') Then ' (' + RTrim ') (convert (char (4), syscolumns.length)) + ')'
When RTrim (Systypes.name) isn't in (' binary ', ' char ', ' nchar ', ' nvarchar ', ' varbinary ', ' varchar ') Then '
End
+ Case When Colorder < @maxcol then ', '
When colorder = @maxcol Then '
End
As type,
Colorder
From syscolumns
Join systypes on syscolumns.xtype = Systypes.xtype
WHERE id = @TableID and systypes.name <> ' sysname '
Union
Select ' as ', @maxcol + 1 as Colorder
Union
Select ' UPDATE ' + @TableName, @maxcol + 2 as Colorder
Union
Select ' SET ', @maxcol + 3 as Colorder
Union
Select Syscolumns.name + ' = @ ' + syscolumns.name
+ Case When Colorder < @maxcol then ', '
When colorder = @maxcol Then '
End
As type,
Colorder + @maxcol + 3 as Colorder
From syscolumns
Join systypes on syscolumns.xtype = Systypes.xtype
WHERE id = @TableID and syscolumns.name <> @PrimaryKey and systypes.name <> ' sysname '
Union
Select ' WHERE ' + @PrimaryKey + ' = @ ' + @PrimaryKey, (2 * @maxcol) + 4 as Colorder
ORDER BY Colorder
Select type from #tempproc order by Colorder
drop table #tempproc
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.