我有一个包含2列的临时表,每列都是我声明的参数。我已经使用这个sql这样做了。
Declare
@SourceKey varchar(40) = '1109'
,@Department Key varchar(1500) = '14,55
然后使用以下sql填充该表:
if OBJECT_ID('Tempdb..#Department','U') is not null
drop table #Department
CREATE TABLE #Department
(DepartmentKey int
,BaseTerm varchar(5))
INSERT INTO #Department
SELECT value
,skt.Key from YYY.ParseList(@Department,',')
join #SourceKeyTable skt
on skt.Key = skt.key
如果我select * From #Department
我得到了这些结果:
Department Key | SourceKey
14 | 1109
55 | 1109
这就是我的期望。然后我将临时表加入到我的主查询中,如此
JOIN #Department d
on Table.rKey = d.DepartmentKey
我需要一个临时表来允许在visual studio报告中进行多选。但是,如果部门键等于14 AND 55,则会导致我的结果出现偏差。我需要1个值传递14 OR 55而不是两个。但是临时表是多选的必要条件。
有关如何在仍然提到之前设置的情况下仅传递1个值的任何建议?
我会尽力回答问题,因为我可能没有足够好地解释这个问题。
答案 0 :(得分:0)
我认为您需要将列表解析为临时表或表变量,然后执行任何需要完成的操作。
很难从代码中看到确切的内容,但下面的代码应该充分说明这个想法。
我创建了一个表变量。插入已解析的列表值,然后循环显示它们,将值打印到输出
--Create a table variable
DECLARE @Departments TABLE(DepartmentOrder int identity, RKey nvarchar(40) NOT NULL)
--Create variables to loop through table variable
DECLARE @DepartmentOrder int
DECLARE @RKey nvarchar(40)
--Populate table variable with the parsed list values
INSERT @Departments (RKey) SELECT RKey from YYY.ParseList(@Department,',')
--Get the first list entry
SELECT @DepartmentOrder = min(DepartmentOrder ) FROM @Departments
--While we've not reached the end
WHILE @DepartmentOrder IS NOT NULL
BEGIN
--Get the Department key for this entry
SET @RKey = (SELECT RKey FROM @Departments WHERE DepartmentOrder = @DepartmentOrder )
--Use the values
PRINT '@DepartmentOrder = '+CONVERT(nvarchar(9),@DepartmentOrder )
PRINT '@RKey = '''+@RKey +''''
--Get the next list entry
SET @DepartmentOrder = (SELECT MIN(DepartmentOrder ) FROM @Departments WHERE DepartmentOrder > @DepartmentOrder )
END