日期:2014-05-16 浏览次数:20574 次
--将数据库中的初始化数据转换成INSERT语句的存储过程
create???? proc?? spGenInsertSQL??
@TableName?? as?? varchar(100)??
as?
--declare?? @TableName?? varchar(100)??
--set?? @TableName?? =?? 'orders'??
--set?? @TableName?? =?? 'eeducation'??
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)??
?
GO
--注意:使用格式:在查询分析器中执行该存储过程(注意切换到对应的数据库)execute spGenInsertSQL 'student',
执行以上语句,则数据库表student中的所有初始化insert语句都会显示在结果集中,复制走即可使用。