Stored Procedures | scripts | data | databases
CREATE Procedure Dbgendata
? @tableName? Varchar (m)?--table name
As
DECLARE @columnName VARCHAR (100)?? --Column Name
DECLARE @TypeName VARCHAR (100)?? --Data type
DECLARE @columns VARCHAR (3000)??? --
DECLARE @columnsAndhead VARCHAR (3000)?--
SET @columnsAndhead = ' INSERT into ' + @tableName + ' ('
SET @columns = '
?--Get all field names for a table
??? DECLARE a? CURSOR for
? SELECT A.[name] As ColumnName, B.[name] as TypeName
? From syscolumns a INNER JOIN systypes b on A.xtype=b.xtype
? where a.[id]= (SELECT [id] from sysobjects where [name]= @tableName)
??? OPEN A
??? FETCH NEXT from a @columnName, @TypeName
??? While @ @FETCH_STATUS = 0
??? BEGIN
?? IF @TypeName in (' bigint ', ' bit ', ' decimal ', ' float ', ' int ', ' money ', ' numeric ', ' real ', ' smallint ', ' smallmoney ', ' Tinyint ')
?? BEGIN
??? SET @columns = @columns + ' ISNULL (CAST (' + @columnName + ' as VARCHAR), ' "' + ', ' + '
?? End
?? ELSE
?? BEGIN
??? SET @columns = @columns +? '''''''''+? ISNULL (CAST (' + @columnName + ' as VARCHAR), ' "') + '" ', ' ' + '
?? End
?? SET @columnsAndhead = @columnsAndhead + @columnName + ', '?????
??????? FETCH NEXT from a @columnName, @TypeName
??? End
? SELECT @columnsAndhead = Left (@columnsAndhead, Len (@columnsAndhead)-1) + ') VALUES (')
? SELECT @columns = Left (@columns, Len (@columns)-5)
??? Close a?
??? Deallocate a??
?
? exec (' SELECT ' + @columnsAndhead + ' + ' + @columns + ' + ') ' as a From ' + @tableName)
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.