2013-09-30 105 views
0

當我試圖執行此存儲過程它給我不是一個有效的標識符錯誤任何人都可以幫助我嗎?錯誤執行存儲過程

CREATE Proc [dbo].[AddLogtrail] 
    @cmodule varchar(100), 
    @caudittype varchar(15),  
    @ctable varchar(100),  
    @cfield varchar(100),  
    @cfieldtype varchar(100), 
    @coriginalval varchar(100),  
    @cnewval varchar(100),  
    @cuser varchar(100),  
    @creason varchar(100)  AS  

    DECLARE @SQL varchar (400)  
    set @SQL = 'Insert into AudittrailLog (ddatetime,cmodulename,caudittype,ctablename,cfieldname,cfieldtype,coriginalval,cnewval,cuser,creason) values ' + 
    '(convert(datetime,GETDATE(),105),'[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+')'; 
    PRINT @SQL 
    Exec @SQL 
+0

當你PRINT @SQL,你得到整個查詢或被截斷? –

+0

我得到整個查詢... INSERT INTO AudittrailLog( \t ddatetime ,cmodulename ,caudittype ,ctablename ,cfieldname ,cfieldtype ,coriginalval ,cnewval ,cuser ,creason)值(轉換(日期時間, GETDATE(),105),測試,測試,測試,測試,測試,測試,測試,測試,測試) –

回答

1

您需要添加額外的報價VARCHAR值

set @SQL = 'Insert into AudittrailLog (ddatetime,cmodulename,caudittype,ctablename,cfieldname,cfieldtype,coriginalval,cnewval,cuser,creason) values ' + 
    '(convert(datetime,GETDATE(),105),'''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''')'; 

打印時它應該顯示這個樣子。

Insert into AudittrailLog (ddatetime ,cmodulename ,caudittype ,ctablename ,cfieldname ,cfieldtype ,coriginalval ,cnewval ,cuser ,creason) values (convert(datetime,GETDATE(),105),'test','test','test','test','test','test','test','test','test') 

另外像下面

EXEC (@SQL) 
+0

當我調用存儲過程它仍然給我一個不是一個有效的標識符,但當我複製打印查詢。它運行.. –

+0

現在它的工作..非常感謝 –

0

你需要用每個varchar在 '執行它':

CREATE Proc [dbo].[AddLogtrail] 
    @cmodule varchar(100), 
    @caudittype varchar(15),  
    @ctable varchar(100),  
    @cfield varchar(100),  
    @cfieldtype varchar(100), 
    @coriginalval varchar(100),  
    @cnewval varchar(100),  
    @cuser varchar(100),  
    @creason varchar(100)  AS  

    DECLARE @SQL varchar (400)  
    set @SQL = 'Insert into AudittrailLog (ddatetime,cmodulename,caudittype,ctablename,cfieldname,cfieldtype,coriginalval,cnewval,cuser,creason) values ' + 
    '(convert(datetime,GETDATE(),105),'''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''','''[email protected]+''')'; 
    PRINT @SQL 
    Exec @SQL 

我希望我沒有錯過任何的'。

0

您的問題只是一個樣本或您的實際程序?

因爲沒有理由對此過程使用動態SQL。你的查詢是靜態的。這是一個簡單的插圖,所有領域都是已知的。爲什麼不直接使用INSERT語句?