Sql 通过单个参数的多个值
此查询显示此错误 Msg 241,16级,状态1,程序示例,第18行Sql 通过单个参数的多个值,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,此查询显示此错误 Msg 241,16级,状态1,程序示例,第18行 从字符串转换日期和/或时间时,转换失败 然而,我尝试了替换和强制转换函数来解决这个问题,但没有找到解决方案,我将使用随附的@params来处理不需要连接到执行字符串的参数。我还建议对@Department进行更多的处理,以防止出现这种情况 SQL Server 2008中最好的方法是为@DepartmentId使用表值参数: SQLZim的解决方案应该能起作用。但既然他没有提到这一点,我会指出错误的原因是这一行:
从字符串转换日期和/或时间时,转换失败
然而,我尝试了替换和强制转换函数来解决这个问题,但没有找到解决方案,我将使用随附的
@params
来处理不需要连接到执行字符串的参数。我还建议对@Department
进行更多的处理,以防止出现这种情况
SQL Server 2008中最好的方法是为
@DepartmentId
使用表值参数:
ALTER PROCEDURE Example
(@BranchCode AS INT,
@Department VARCHAR(8000),
@DateFrom AS DATETIME,
@DateTo AS DATETIME,
@TransactionNumber AS NVARCHAR(30)
-- @Delimiter char(1)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Sql NVARCHAR(MAX), @Params nvarchar(8000);
set @Params = '@BranchCode INT, @DateFrom DATETIME, @DateTo DATETIME, @TransactionNumber NVARCHAR(30)'
SET @Sql = 'Select
Mast.Branch_code, Mast.purerq_id as purerq_id,
Max(Mast.purreq_date) as purreq_date, Max(Mast.Dept_code) as Dept_code,
Max(Mast.purreq_desc) as purreq_desc,
Sum(Det.purreq_qty2) as purreq_qty2,
Det.Item_Code, Max(Mast.purreq_docecNo) as DocNo,
Sum(IsNull(Det.purreq_qty1,0)) as purreq_qty1,
IsNull(Det.purerq_TranNo,'') as purerq_TranNo,
IsNull(Max(Mast.purerq_adduserid),'') as Adduserid,
Max(Det.ItemPacking_Code) as ItemPacking_Code
From tbl_purreqMaster Mast
inner join tbl_purreq_detail1 Det
on Mast.Branch_code = Det.Branch_code
and Mast.purerq_id = Det.purerq_id
Where Mast.purreq_date Between @DateFrom and @DateTo
and Mast.Dept_code IN (' + REPLACE( @Department, '''','') + ')
and Mast.Branch_code = @BranchCode
and Mast.purreq_docecNo = @TransactionNumber
Group By
Mast.Branch_code, Mast.purerq_id, Det.Item_Code, Det.purerq_TranNo'
PRINT @Sql
--exec sp_executesql @Sql, @Params, @BranchCode, @DateFrom, @DateTo, @TransactionNumber
END
您需要将日期时间变量强制转换为nvarchar,并用单引号将其括起来。-在ANSI-92 SQL标准(25年前)中,用正确的ANSI
JOIN
语法替换了旧样式的逗号分隔表样式,不鼓励使用该样式。谢谢您的指导,我的问题由你的向导解决了
ALTER PROCEDURE Example
(@BranchCode AS INT,
@Department VARCHAR(8000),
@DateFrom AS DATETIME,
@DateTo AS DATETIME,
@TransactionNumber AS NVARCHAR(30)
-- @Delimiter char(1)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Sql NVARCHAR(MAX), @Params nvarchar(8000);
set @Params = '@BranchCode INT, @DateFrom DATETIME, @DateTo DATETIME, @TransactionNumber NVARCHAR(30)'
SET @Sql = 'Select
Mast.Branch_code, Mast.purerq_id as purerq_id,
Max(Mast.purreq_date) as purreq_date, Max(Mast.Dept_code) as Dept_code,
Max(Mast.purreq_desc) as purreq_desc,
Sum(Det.purreq_qty2) as purreq_qty2,
Det.Item_Code, Max(Mast.purreq_docecNo) as DocNo,
Sum(IsNull(Det.purreq_qty1,0)) as purreq_qty1,
IsNull(Det.purerq_TranNo,'') as purerq_TranNo,
IsNull(Max(Mast.purerq_adduserid),'') as Adduserid,
Max(Det.ItemPacking_Code) as ItemPacking_Code
From tbl_purreqMaster Mast
inner join tbl_purreq_detail1 Det
on Mast.Branch_code = Det.Branch_code
and Mast.purerq_id = Det.purerq_id
Where Mast.purreq_date Between @DateFrom and @DateTo
and Mast.Dept_code IN (' + REPLACE( @Department, '''','') + ')
and Mast.Branch_code = @BranchCode
and Mast.purreq_docecNo = @TransactionNumber
Group By
Mast.Branch_code, Mast.purerq_id, Det.Item_Code, Det.purerq_TranNo'
PRINT @Sql
--exec sp_executesql @Sql, @Params, @BranchCode, @DateFrom, @DateTo, @TransactionNumber
END
Mast.purreq_date Between ' + @DateFrom + ' And ' + @DateTo + ' And