server| Scripts | Data uses SQL Server 2000 's own build SQL Scripts tool to generate SQL scripts to create tables, views, stored procedures, and so on. So can the data in the table also be generated as SQL scripts that automatically import data into SQL Server after executing the scripts in Query Analyzer? The answer is yes. The following stored procedure is written by an expert, and the man's surname is no longer known, but this monumental work is occasionally seen in the SQL Server community. CREATE PROCEDURE dbo. Outputdata
@tablename sysname
As
DECLARE @column varchar (1000)
DECLARE @columndata varchar (1000)
DECLARE @sql varchar (4000)
DECLARE @xtype tinyint
declare @name sysname
DECLARE @objectId int
declare @objectname sysname
DECLARE @ident int
SET NOCOUNT ON
Set @objectId =object_id (@tablename)
If @objectId is null--Determines whether an object exists
Begin
Print @tablename + ' object does not exist '
Return
End Set @objectname =rtrim (object_name (@objectId))
If @objectname is null or CHARINDEX (@objectname, @tablename) =0
Begin
Print @tablename + ' object not in current database '
Return
End If OBJECTPROPERTY (@objectId, ' istable ') < > 1--Determine if the object is a table
Begin
Print @tablename + ' object is not a table '
Return
End Select @ident =status&0x80 from syscolumns where id= @objectid and status&0x80=0x80 If @ident is not null
print ' SET identity_insert ' + @TableName + ' on '--defines a cursor, iterates through the data, and generates an INSERT statement
DECLARE syscolumns_cursor cursor FOR
Select C.name,c.xtype from syscolumns C
where c.id= @objectid
ORDER BY C.colid--open cursor
Open Syscolumns_cursor
Set @column = '
Set @columndata = '
FETCH NEXT from Syscolumns_cursor into @name, @xtype
While @ @fetch_status <>-1
Begin
If @ @fetch_status <>-2
Begin
If @xtype not in (189,34,35,99,98)--timestamp does not need to be processed, image,text,ntext,sql_variant temporarily does not handle
Begin
Set @column = @column +
case when Len (@column) =0 Then '
Else ', '
End + @name
Set @columndata = @columndata +
case when Len (@columndata) =0 Then '
Else ', ', ', ', '
End +
When the case is @xtype in (167,175) Then ' ' + ' + @name + ' + ' "'--varchar,char
When @xtype in (231,239) Then ' ' N ' ' ' ' + ' + ' + ' + ' + ' + ' + ' + ' + ' + ' + ' + ' + ' + ' "'--nvarchar,nchar
When @xtype =61 then ' "' +convert (char), ' + @name + ', 121) + '" ""--datetime
When @xtype =58 then ' "' +convert (char), ' + @name + '," + "" "" "--smalldatetime
When @xtype =36 then ' "" ' +convert (char), ' + @name + ') + ' "'--uniqueidentifier
else @name
End
End
End
FETCH NEXT from Syscolumns_cursor into @name, @xtype
End
Close Syscolumns_cursor
deallocate syscolumns_cursor Set @sql = ' Set NOCOUNT on select ' Insert ' + @tablename + ' (' + @column + ') VALUES (' as '--', ' + @columndata + ', ') ' ' from ' + @tablename print '--' + @sql
EXEC (@sql) If @ident is not null
print ' SET identity_insert ' + @TableName + ' off ' call exec outputdata ' myuser ' where tables exist in the current database in MyUser
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.