我搜索了&得到了下面的存儲過程以生成帶有數據的插入語句。問題是這個存儲過程無法處理單引號。在sql server中生成帶有數據的插入語句
假設如果數據中有任何單引號,那麼插入語句會生成,但執行插入語句時會出現錯誤。所以引導我在程序中修復的地方,因爲它可以處理任何單引號或任何特殊字符。
感謝
CREATE PROC InsertGenerator
(@tableName varchar(100)) as
--Declare a cursor to retrieve column specific information for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR
SELECT column_name,data_type FROM information_schema.columns WHERE table_name = @tableName
OPEN cursCol
DECLARE @string nvarchar(3000) --for storing the first half of INSERT statement
DECLARE @stringData nvarchar(3000) --for storing the data (VALUES) related statement
DECLARE @dataType nvarchar(1000) --data types returned for respective columns
SET @string='INSERT '[email protected]+'('
SET @stringData=''
DECLARE @colName nvarchar(50)
FETCH NEXT FROM cursCol INTO @colName,@dataType
IF @@fetch_status<>0
begin
print 'Table '[email protected]+' not found, processing skipped.'
close curscol
deallocate curscol
return
END
WHILE @@FETCH_STATUS=0
BEGIN
IF @dataType in ('varchar','char','nchar','nvarchar')
BEGIN
--SET @[email protected]+'''''''''+isnull('[email protected]+','''')+'''''',''+'
SET @[email protected]+''''+'''+isnull('''''+'''''+'[email protected]+'+'''''+''''',''NULL'')+'',''+'
END
ELSE
if @dataType in ('text','ntext') --if the datatype is text or something else
BEGIN
SET @[email protected]+'''''''''+isnull(cast('[email protected]+' as varchar(2000)),'''')+'''''',''+'
END
ELSE
IF @dataType = 'money' --because money doesn't get converted from varchar implicitly
BEGIN
SET @[email protected]+'''convert(money,''''''+isnull(cast('[email protected]+' as varchar(200)),''0.0000'')+''''''),''+'
END
ELSE
IF @dataType='datetime'
BEGIN
--SET @[email protected]+'''convert(datetime,''''''+isnull(cast('[email protected]+' as varchar(200)),''0'')+''''''),''+'
--SELECT 'INSERT Authorizations(StatusDate) VALUES('+'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM Authorizations
--SET @[email protected]+'''convert(money,''''''+isnull(cast('[email protected]+' as varchar(200)),''0.0000'')+''''''),''+'
SET @[email protected]+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'[email protected]+',121)+'''''+''''',''NULL'')+'',121),''+'
-- 'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM Authorizations
END
ELSE
IF @dataType='image'
BEGIN
SET @[email protected]+'''''''''+isnull(cast(convert(varbinary,'[email protected]+') as varchar(6)),''0'')+'''''',''+'
END
ELSE --presuming the data type is int,bit,numeric,decimal
BEGIN
--SET @[email protected]+'''''''''+isnull(cast('[email protected]+' as varchar(200)),''0'')+'''''',''+'
--SET @[email protected]+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'[email protected]+',121)+'''''+''''',''NULL'')+'',121),''+'
SET @[email protected]+''''+'''+isnull('''''+'''''+convert(varchar(200),'[email protected]+')+'''''+''''',''NULL'')+'',''+'
END
SET @[email protected][email protected]+','
FETCH NEXT FROM cursCol INTO @colName,@dataType
END
DECLARE @Query nvarchar(4000)
SET @query ='SELECT '''+substring(@string,0,len(@string)) + ') VALUES(''+ ' + substring(@stringData,0,len(@stringData)-2)+'''+'')'' FROM '[email protected]
exec sp_executesql @query
--select @query
CLOSE cursCol
DEALLOCATE cursCol
calling like sp-name 'table name'
InsertGenerator 'mytable'
這是錯誤....現在修復。 plzz有看。謝謝 – Thomas
請參閱http://stackoverflow.com/questions/982568/what-is-the-best-way-to-auto-generate-insert-statements-for-a-sql-server-table - Shane Fulmer的答案顯示了一個存儲過程去做這個。不確定它是否正確處理單引號。否則搜索谷歌!你會發現**噸的鏈接**來探索! –
您可以將值作爲參數傳遞給'sp_executesql'而不是連接。 –