使用datetime变量执行动态sql查询时出错

时间:2014-07-16 06:16:03

标签: sql-server stored-procedures

表格创建:(tbl1& tbl2)

CREATE TABLE tbl1
( 
    EmpID               int         , 
    EmpName             varchar(20)     , 
    Experience                    int           ,
    Roll                varchar(30)     ,
    DOJ             datetime                  ,
    PRIMARY KEY(EmpID,DOJ)    
); 

CREATE TABLE tbl2
( 
    ProjID              int     ,
    ProjName                      varchar(40)   , 
    ProjDuration                int     ,
    EmpID               int     ,
    DOJ             datetime              ,
    PRIMARY KEY(ProjName,EmpID,DOJ)
);

tbl1值:

   EmpID        EmpName           Experience        Roll       DOJ

11  aaa     NULL        PAT 2013-11-22 00:00:00.000
22  bbb     2       PA  2012-05-16 00:00:00.000
33  eee     NULL        A   2008-12-10 00:00:00.000
44  NULL        3       NULL    2011-06-26 00:00:00.000
55  ddd     NULL        NULL    2010-08-12 00:00:00.000
66  ccc     1       NULL    2013-05-02 00:00:00.000
77  NULL        NULL        SA  2006-12-01 00:00:00.000

tbl2值:

ProjID   ProjName        ProjDuration           EmpID       DOJ

NULL       3M       2       33  2008-12-10 00:00:00.000
2002       3M       NULL        55  2010-08-12 00:00:00.000
3003       MPC      3       11  2013-11-22 00:00:00.000
NULL       MPC      NULL        66  2013-05-02 00:00:00.000
3003       MPC      3       77  2006-12-01 00:00:00.000
1001       Shell              NULL      22  2012-05-16 00:00:00.000
1001       Shell              4     44  2011-06-26 00:00:00.000

存储过程以检索非空的列:(使用EmpID和DOJ作为参数)

GO
CREATE PROCEDURE sp_Sample
    @EmpID                  INT     ,
    @DOJ                    DATETIME        
AS 
BEGIN

DECLARE @query nVARCHAR(1000)
DECLARE @T_EmpID nVARCHAR(5)
DECLARE @T_DOJ nVARCHAR(15)
SET @T_EmpID = convert(varchar(5), @EmpID);
SET @T_DOJ = convert(varchar(15), @DOJ);
SET @query = 'select '''+@T_EmpID+''' as EmployeeID';
IF((select EmpName from tbl1 where EmpID = @T_EmpID and DOJ = @T_DOJ) is not null)
    SET @query += ',EmpName';
IF((select Experience from tbl1 where EmpID = @T_EmpID and DOJ = @T_DOJ) is not null)
    SET @query += ',Experience';
IF((select Roll from tbl1 where EmpID = @T_EmpID and DOJ = @T_DOJ) is not null)
      SET @query += ',Roll';
IF((select ProjID from tbl2 where EmpID = @T_EmpID and DOJ = @T_DOJ) is not null)
    SET @query += ',ProjID';
IF((select ProjName from tbl2 where EmpID = @T_EmpID and DOJ = @T_DOJ) is not null)
    SET @query += ',ProjName';
IF((select ProjDuration from tbl2 where EmpID = @T_EmpID and DOJ = @T_DOJ) is not null)
    SET @query += ',ProjDuration';
SET @query += ' from tbl1 a join tbl2 b on a.EmpID = b.EmpID and a.DOJ = b.DOJ where a.EmpID = '+@T_EmpID+'and a.DOJ = '+@T_DOJ 

IF (@query IS NOT NULL)
Exec (@query)

END

EXEC sp_Sample '55','2010-08-12 00:00:00.000'

执行存储过程时出错:

消息241,级别16,状态1,过程sp_Sample,第13行

从字符串转换日期和/或时间时转换失败。

任何人都可以帮我解决这个错误吗??? 提前致谢

2 个答案:

答案 0 :(得分:0)

按以下格式更改您的存储过程:

ALTER PROCEDURE sp_Sample
    @EmpID                  INT     ,
    @DOJ                    DATETIME        
AS 
BEGIN

DECLARE @query nVARCHAR(1000)
DECLARE @T_EmpID nVARCHAR(5)
DECLARE @T_DOJ nVARCHAR(15)
SET @T_EmpID = convert(varchar(5), @EmpID);
SET @T_DOJ = convert(varchar(15), @DOJ,111);
SET @query = 'select '''+@T_EmpID+''' as EmployeeID';
IF((select EmpName from tbl1 where EmpID = @T_EmpID and DOJ = @DOJ) is not null)
    SET @query += ',EmpName';
IF((select Experience from tbl1 where EmpID = @T_EmpID and DOJ = @DOJ) is not null)
    SET @query += ',Experience';
IF((select Roll from tbl1 where EmpID = @T_EmpID and DOJ = @DOJ) is not null)
      SET @query += ',Roll';
IF((select ProjID from tbl2 where EmpID = @T_EmpID and DOJ = @DOJ) is not null)
    SET @query += ',ProjID';
IF((select ProjName from tbl2 where EmpID = @T_EmpID and DOJ = @DOJ) is not null)
    SET @query += ',ProjName';
IF((select ProjDuration from tbl2 where EmpID = @T_EmpID and DOJ = @DOJ) is not null)
    SET @query += ',ProjDuration';
SET @query += ' from tbl1 a join tbl2 b on a.EmpID = b.EmpID and a.DOJ = b.DOJ where a.EmpID = '+@T_EmpID+'and a.DOJ = '''+@T_DOJ +''''

IF (@query IS NOT NULL)
Exec (@query)
END
GO

EXEC sp_Sample '55','2010-08-12 00:00:00.000'

我的改变是:

  1. SET @T_DOJ = convert(varchar(15),@ DOJ,111);
  2. ...和DOJ = @DOJ(在所有条件下)

答案 1 :(得分:0)

首先将字符串转换为时间戳,然后比较/分配

而不是

DOJ = @T_DOJ

尝试

DOJ = to_TIMESTAMP(@T_DOJ,'YYYY-MM-DD HH:MI:SS:FF')"
or 
DOJ = TIMESTAMP_FORMAT(@T_DOJ,'YYYY-MM-DD HH:MI:SS:FF')"

上面使用的函数适用于DB2。对正在使用的DB服务器使用等效/兼容功能。