电脑技术学习

SQL SERVER数据导成INSERT

dn001

create;;procspGenInsertSQL
@TableNameasvarchar(100)
as
--declare@TableNamevarchar(100)
--set@TableName='orders'
--set@TableName='eeducation'
DECLARExCursorCURSORFOR
SELECTname,xusertype
FROMsyscolumns
WHERE(id=OBJECT_ID(@TableName))
declare@F1varchar(100)
declare@F2integer
declare@SQLvarchar(8000)
set@sql='SELECT''INSERTINTO'+@TableName+'VALUES('''
OPENxCursor
FETCHxCursorinto@F1,@F2
WHILE@@FETCH_STATUS=0
BEGIN
set@sql=@sql +
+casewhen@F2IN(35,58,99,167,175,231,239,61)then'+casewhen'+@F1+'ISNULLthen''''else''''''''end+';;else'+'end
+'replace(ISNULL(cast('+@F1+'asvarchar(8000)),''NULL''),'''''''','''''''''''')';;
+casewhen@F2IN(35,58,99,167,175,231,239,61)then'+casewhen'+@F1+'ISNULLthen''''else''''''''end+';;else'+'end
+char(13)+''',''';;
FETCHNEXTFROMxCursorinto@F1,@F2
END
CLOSExCursor
DEALLOCATExCursor
set@sql=left(@sql,len(@sql)-5)+'+'')''FROM'+@TableName
exec(@sql)

go

EXEC spGenInsertSQL *TBLNAME*