获取表数据的插入SQL

DECLARE @TABLE_NAME VARCHAR(200) SET @TABLE_NAME = 'myFunction'   --表名
DECLARE @TABLE_CONDITION VARCHAR(200) SET @TABLE_CONDITION = 'where Application=''0303''  AND  FunctionType=''功能'''    --条件

DECLARE @sql VARCHAR(5000)  SET @sql=''
DECLARE @col VARCHAR(5000)  SET @col=''
DECLARE @scol VARCHAR(5000) SET @scol=''

SELECT @col=@col+','+NAME
       ,@scol=@scol+',''''''+ISNULL(convert(varchar(5000),'+NAME+'),'''')+''''''' 
from syscolumns 
Where ID=OBJECT_ID(@TABLE_NAME) 

SET @col=RIGHT(@col,LEN(@col)-1)
SET @scol=substring(@scol,2,LEN(@scol)-1)+'''' 
--SELECT  @col,@scol

set @sql = 'SELECT  ''
insert into '+ @TABLE_NAME +'('+@col+') values (' +  @scol +' +'')'' 
from '+@TABLE_NAME+' ' + @TABLE_CONDITION
   

EXEC (@sql)

posted @ 2017-04-28 18:37  lytwajue  阅读(235)  评论(0编辑  收藏  举报