Generate inserts based on basic table structure and data ... The SQL
Source: Internet
Author: User
Data
Create proc Spgeninsertsql
@TableName as varchar (100)
As
--declare @TableName varchar (100)
--set @TableName = ' orders '
--set @TableName = ' eeducation '
DECLARE Xcursor CURSOR for
SELECT Name,xusertype
From syscolumns
WHERE (id = object_id (@TableName))
DECLARE @F1 varchar (100)
DECLARE @F2 integer
DECLARE @SQL varchar (8000)
Set @sql = ' SELECT ' INSERT into ' + @TableName + ' VALUES ('
OPEN Xcursor
FETCH xcursor into @F1, @F2
While @ @FETCH_STATUS = 0
BEGIN
Set @sql = @sql +
+ Case when @F2 into (35,58,99,167,175,231,239,61) then ' + case ' + @F1 + ' is NULL then ' ' Else ' ' ' LSE ' + ' end
+ ' replace (ISNULL (' + @F1 + ' as varchar '), ' ' NULL ' '), ' ' ', ' ', ' ' '
+ Case when @F2 into (35,58,99,167,175,231,239,61) then ' + case ' + @F1 + ' is NULL then ' ' Else ' ' ' LSE ' + ' end
+ char (13) + ', '
FETCH NEXT from Xcursor into @F1, @F2
End
Close Xcursor
Deallocate xcursor
Set @sql = Left (@sql, Len (@sql)-5) + ' + ' "' from ' + @TableName
Print @sql
EXEC (@sql)
Second Edition: 2003.03.08
ALTER proc SPGENINSERTSQL (@tablename varchar (256))
As
Begin
DECLARE @sql varchar (8000)
DECLARE @sqlValues varchar (8000)
Set @sql = ' ('
Set @sqlValues = ' VALUES (' + '
Select @sqlValues = @sqlValues + cols + ' + ', ' + ', @sql = @sql + ' [' + name + '], '
From
(Select Case
When xtype in (48,52,56,59,60,62,104,106,108,122,127)
Then ' case when ' + name + ' is null then ' null ' else ' + ' cast (' + name + ' as varchar) ' + ' end '
When xtype in (58,61)
Then ' case when ' + name + ' is null then ' ' null ' else ' + ' ' ' + ' + ' ' + ' ' + ' ' + ' + ' as varchar ' + ' + ' '
When xtype in (167)
Then ' case when ' + name + ' is null then ' null ' else ' + ' ' + ' ' + ' + ' replace (' + name+ ', ' ' ', ' ', ' ', ' ', ' ' ', ' ' ' + ' + ' "" ' + ' end '
When xtype in (231)
Then ' case if ' + name + ' is null then ' null ' ' Else ' + ' ' N ' ' ' + ' ' + ' + ' replace (' + name+ ', ' ' ', ', ' ', ' ', ' ', ' ' ' + ' + ' "" ' + ' end '
When xtype in (175)
Then ' case when ' + name + ' is null then ' null ' ' Else ' + ' ' + ' + ' cast (replace (' + name+ ', ' ' ', ', ' ', ' ', ' ' Char (' + cast (length as varchar) + ')) + ' "', ' + ' end '
When xtype in (239)
Then ' case when ' + name + ' is null then ' null ' else ' + ' ' N ' ' + ' ' + ' + ' cast (replace (' + name+ ', ' ' ', ', ' ', ' ', ' ' Char (' + cast (length as varchar) + ')) + ' "', ' + ' end '
Else ' ' NULL '
End as Cols,name
From syscolumns
WHERE id = object_id (@tablename)
) T
Set @sql = ' SELECT ' INSERT into [' + @tablename + '] ' + left (@sql, Len (@sql)-1) + ' "+ Left (@sqlValues, Len (@sqlValues)-4) + ' From ' + @tablename
--print @sql
EXEC (@sql)
End
Third edition: 2003.3.9
ALTER proc Spgeninsertsql (@tablename varchar (256))
As
Begin
DECLARE @sql varchar (8000)
DECLARE @sqlValues varchar (8000)
Set @sql = ' ('
Set @sqlValues = ' VALUES (' + '
Select @sqlValues = @sqlValues + cols + ' + ', ' + ', @sql = @sql + ' [' + name + '], '
From
(Select Case
When xtype in (48,52,56,59,60,62,104,106,108,122,127)
Then ' case when ' + name + ' is null then ' null ' else ' + ' cast (' + name + ' as varchar) ' + ' end '
When xtype in (58,61)
Then ' case when ' + name + ' is null then ' ' null ' else ' + ' ' ' + ' + ' ' + ' ' + ' ' + ' + ' as varchar ' + ' + ' '
When xtype in (167,175)
Then ' case when ' + name + ' is null then ' null ' else ' + ' ' + ' ' + ' + ' replace (' + name+ ', ' ' ', ' ', ' ', ' ', ' ' ', ' ' ' + ' + ' "" ' + ' end '
When xtype in (231,239)
Then ' case if ' + name + ' is null then ' null ' ' Else ' + ' ' N ' ' ' + ' ' + ' + ' replace (' + name+ ', ' ' ', ', ' ', ' ', ' ', ' ' ' + ' + ' "" ' + ' end '
Else ' ' NULL '
End as Cols,name
From syscolumns
WHERE id = object_id (@tablename) and autoval is null
) T
Set @sql = ' SELECT ' INSERT into [' + @tablename + '] ' + left (@sql, Len (@sql)-1) + ' "+ Left (@sqlValues, Len (@sqlValues)-4) + ' From ' + @tablename
Print @sql
EXEC (@sql)
/*
SELECT *
From syscolumns
WHERE id = object_id (' Test ') and Autoval is null
*/
End
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service