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),'+@cmodule+','+@caudittype+','+@ctable+','+@cfield+','+@cfieldtype+','+@coriginalval+','+@cnewval+','+@cuser+','+@creason+')';
    PRINT @SQL
    Exec @SQL
4

3 回答 3

1

您需要为 varchar 值添加额外的引号

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

打印时应该是这样显示的。

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)
于 2013-09-30T06:27:13.490 回答
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),'''+@cmodule+''','''+@caudittype+''','''+@ctable+''','''+@cfield+''','''+@cfieldtype+''','''+@coriginalval+''','''+@cnewval+''','''+@cuser+''','''+@creason+''')';
    PRINT @SQL
    Exec @SQL

我希望我没有错过任何一个''。

于 2013-09-30T06:28:42.000 回答
0

您的问题只是一个示例还是您的实际程序?

因为没有理由为此过程使用动态 SQL。您的查询是静态的。这是一个简单的插图,所有字段都是已知的。为什么不直接使用 INSERT 语句?

于 2013-09-30T06:39:13.487 回答