首页 > 代码库 > 获取表数据的插入SQL

获取表数据的插入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)