SQL server--script that generates table data through stored procedures

Source: Internet
Author: User

CREATE PROCEDURE dbo. uspoutputdata @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 ' The object Not EXISTS '

Return

End

Set @objectname =rtrim (object_name (@objectId))

If @objectname is null or CHARINDEX (@objectname, @tablename) =0--This judgment is not tight

Begin

Print ' object not in current database '

Return

End

  

If OBJECTPROPERTY (@objectId, ' istable ') < > 1--Determine if the object is a table

Begin

print ' The object is not table '

Return

End

  

Select @ident =status&0x80 from syscolumns where [email protected] and status&0x80=0x80

  

If @ident is not null

print ' SET identity_insert ' [email protected]+ ' on '

  

DECLARE syscolumns_cursor cursor

  

For select C.name,c.xtype from syscolumns c where [e-mail protected] ORDER by C.colid

  

Open Syscolumns_cursor

Set @column = ' '

Set @columndata = ' '

FETCH NEXT from Syscolumns_cursor to @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 process

Begin

Set @[email protected]+case when Len (@column) =0 then ' Else ', ' [email protected]

Set @[email protected]+case when Len (@columndata) =0 then ' Else ', ' ', ' ', '

End

+case when @xtype into (167,175) then "" "" + "[email protected]+ ' + '" "" "--varchar,char

When @xtype in (231,239) then "N" "+" [email protected]+ ' + ' "" "--nvarchar,nchar

When @xtype =61 and then "" "+convert (char), ' [email protected]+ ', 121] + '" ""--datetime

When @xtype =58 and then "" +convert (char), ' [email protected]+ ', + + '] + ' "'--smalldatetime

When @xtype =36 and then "" "+convert (char), ' [email protected]+ ') + '" "'--uniqueidentifier

else @name End

End

End

FETCH NEXT from Syscolumns_cursor to @name, @xtype

End

Close Syscolumns_cursor

Deallocate syscolumns_cursor

Set @sql = ' Set NOCOUNT on select ' ' Insert ' [email protected]+ ' (' [email protected]+ ') VALUES (' as '--', ' [email protected] + ', ') ' from ' [email protected]

print '--' [email protected]

EXEC (@sql)

If @ident is not null

print ' SET identity_insert ' [email protected]+ ' OFF '

GO

EXEC uspoutputdata your table name

SQL server--script that generates table data through stored procedures

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.