2014-04-11 92 views
0

我搜索了&得到了下面的存儲過程以生成帶有數據的插入語句。問題是這個存儲過程無法處理單引號。在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' 
+0

這是錯誤....現在修復。 plzz有看。謝謝 – Thomas

+0

請參閱http://stackoverflow.com/questions/982568/what-is-the-best-way-to-auto-generate-insert-statements-for-a-sql-server-table - Shane Fulmer的答案顯示了一個存儲過程去做這個。不確定它是否正確處理單引號。否則搜索谷歌!你會發現**噸的鏈接**來探索! –

+0

您可以將值作爲參數傳遞給'sp_executesql'而不是連接。 –

回答

0

什麼是你想實現什麼?如果您需要定期將數據移動到不同的數據庫,我認爲您最好考慮將數據導出到文件然後導入它。或者只是使用簡單的INSERT .. SELECT聲明。如果這只是一次性任務,我會使用管理工作室來生成插入站點,而不是爲此編寫存儲過程。

只需右鍵單擊數據庫。導航至Tasks->Generate Scripts...。選擇要爲其生成插入的表格。然後在Set Scripting Options選項卡上點擊Advaced按鈕並將Types of data to script屬性設置爲Data only。這將生成插入語句處理所有棘手的事情,如轉義單引號。

希望它能幫助!

+0

我正在尋找完整的證明存儲過程,可以處理單引號數據和任何其他特殊字符。 – Thomas