把表里的数据转换为insert 语句

当表里面有数据时,怎么把表里的数据转换为insert 语句

(从别人那里看来的用SQLServer 2008 R2测试可用)

CREATE PROC spGenInsertSQL @TableName AS VARCHAR(100)
AS
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 IN (
35
,58
,99
,167
,175
,231
,239
,61
)
THEN '    +    case    when    ' + @F1 + '    IS    NULL    then    ''''    else    ''''''''    end    +    '
ELSE '+'
END + 'replace(ISNULL(cast(' + @F1 + '    as    varchar(8000)),''NULL''),'''''''','''''''''''')' + CASE
WHEN @F2 IN (
35
,58
,99
,167
,175
,231
,239
,61
)
THEN '    +    case    when    ' + @F1 + '    IS    NULL    then    ''''    else    ''''''''    end    +    '
ELSE '+'
END + CHAR(13) + ''','''

FETCH NEXT
FROM xCursor
INTO @F1
,@F2
END

CLOSE xCursor

DEALLOCATE xCursor

SET @sql = left(@sql, len(@sql) - 5) + '    +    '')''    FROM    ' + @TableName

EXEC (@sql)
GOEXEC    spGenInsertSQL    tbl_Message            --tablename

上一篇:js 键盘记录实现(兼容FireFox和IE)(转)


下一篇:JavaScript之this,new,delete,call,apply