Tsql where子句中包含多个值的大小写表达式
我有多个具有单个标识码的客户端和两个具有多个标识符的客户端。我试图找到一种好方法,将客户机代码用作where子句中单个和多个代码的存储过程的参数 我目前有一个变量,它接受输入的参数并将其转换为单个字符(客户机代码),但使用带有“in”的变量不起作用。我还尝试将客户机代码格式化为where子句中的case表达式,但无法确定其格式Tsql where子句中包含多个值的大小写表达式,tsql,Tsql,我有多个具有单个标识码的客户端和两个具有多个标识符的客户端。我试图找到一种好方法,将客户机代码用作where子句中单个和多个代码的存储过程的参数 我目前有一个变量,它接受输入的参数并将其转换为单个字符(客户机代码),但使用带有“in”的变量不起作用。我还尝试将客户机代码格式化为where子句中的case表达式,但无法确定其格式 ALTER PROCEDURE [dbo].[client_tonnage] @input varchar(100) as select input = cas
ALTER PROCEDURE [dbo].[client_tonnage] @input varchar(100)
as
select input =
case
when @input = 'client1' then 'C1'
when @input = 'client2' then '''C2'', ''C3'', ''C4'''
if object_id('tempdb..#temp_table') is not null drop table #temp_table
insert into #temp_table (date_time, clientid, ACTUAL_WEIGHT)
from Log
where clientid in (@input)
select * from #temp_table
我希望能够包括多个客户端代码的客户端,但上面的格式不起作用。任何解决办法都将不胜感激 您正在做:
select input =
这与参数不同,因为缺少@
符号
你想要:
select @input =
case
when @input = 'client1' then 'C1'
when @input = 'client2' then '''C2'', ''C3'', ''C4'''
你正在做:
select input =
这与参数不同,因为缺少@
符号
你想要:
select @input =
case
when @input = 'client1' then 'C1'
when @input = 'client2' then '''C2'', ''C3'', ''C4'''
如果要保存多个值,请使用表变量:
ALTER PROCEDURE [dbo].[client_tonnage] @input varchar(100)
as
BEGIN
DECLARE @ClientIDs TABLE (ClientID VARCHAR(10))
IF @input = 'client1'
INSERT INTO @ClientIDs (ClientID) VALUES ('C1')
ELSE IF @input = 'client2'
INSERT INTO @ClientIDs (ClientID) VALUES ('C2'),('C3'),('C4')
if object_id('tempdb..#temp_table') is not null
drop table #temp_table
insert into #temp_table (date_time, clientid, ACTUAL_WEIGHT)
SELECT
-- The proper columns
from
Log AS L
INNER JOIN @ClientIDs AS C ON L.clientid = C.ClientID
select * from #temp_table
END
尽管您正在硬编码输入和客户端ID之间的“映射”。更好的方法是将此关系存储在物理表中:
CREATE TABLE ClientMappings (
ClientID VARCHAR(10) PRIMARY KEY,
ClientCode VARCHAR(10))
INSERT INTO ClientMappings (
ClientID,
ClientCode)
VALUES
('C1', 'client1'),
('C2', 'client2'),
('C3', 'client2'),
('C4', 'client2')
因此,您可以通过简单的联接来解决此问题:
ALTER PROCEDURE [dbo].[client_tonnage] @input varchar(100)
as
BEGIN
if object_id('tempdb..#temp_table') is not null
drop table #temp_table
insert into #temp_table (date_time, clientid, ACTUAL_WEIGHT)
SELECT
-- The proper columns
from
Log AS L
INNER JOIN ClientMappings AS C ON L.clientid = C.ClientID
WHERE
C.clientcode = @input
select * from #temp_table
END
此外,如果您的SP只是选择此数据,则不需要临时表。如果要保存多个值,请使用表变量:
ALTER PROCEDURE [dbo].[client_tonnage] @input varchar(100)
as
BEGIN
DECLARE @ClientIDs TABLE (ClientID VARCHAR(10))
IF @input = 'client1'
INSERT INTO @ClientIDs (ClientID) VALUES ('C1')
ELSE IF @input = 'client2'
INSERT INTO @ClientIDs (ClientID) VALUES ('C2'),('C3'),('C4')
if object_id('tempdb..#temp_table') is not null
drop table #temp_table
insert into #temp_table (date_time, clientid, ACTUAL_WEIGHT)
SELECT
-- The proper columns
from
Log AS L
INNER JOIN @ClientIDs AS C ON L.clientid = C.ClientID
select * from #temp_table
END
尽管您正在硬编码输入和客户端ID之间的“映射”。更好的方法是将此关系存储在物理表中:
CREATE TABLE ClientMappings (
ClientID VARCHAR(10) PRIMARY KEY,
ClientCode VARCHAR(10))
INSERT INTO ClientMappings (
ClientID,
ClientCode)
VALUES
('C1', 'client1'),
('C2', 'client2'),
('C3', 'client2'),
('C4', 'client2')
因此,您可以通过简单的联接来解决此问题:
ALTER PROCEDURE [dbo].[client_tonnage] @input varchar(100)
as
BEGIN
if object_id('tempdb..#temp_table') is not null
drop table #temp_table
insert into #temp_table (date_time, clientid, ACTUAL_WEIGHT)
SELECT
-- The proper columns
from
Log AS L
INNER JOIN ClientMappings AS C ON L.clientid = C.ClientID
WHERE
C.clientcode = @input
select * from #temp_table
END
此外,如果您的SP只是选择此数据,则不需要临时表。您可以通过将所有逻辑放入
案例中来使用案例
表达式。请注意,case
表达式返回一个值,然后必须对该值进行处理,例如,将其与一个值进行比较
insert into #temp_table (date_time, clientid, ACTUAL_WEIGHT)
from Log
where case
when @input = 'client1' and clientid = 'C1' then 1
when @input = 'client2' and clientid in ( 'C2', 'C3', 'C4' ) then 1
else 0 end = 1;
这可能会受到影响。您可以通过将所有逻辑放在案例中来使用案例
表达式。请注意,case
表达式返回一个值,然后必须对该值进行处理,例如,将其与一个值进行比较
insert into #temp_table (date_time, clientid, ACTUAL_WEIGHT)
from Log
where case
when @input = 'client1' and clientid = 'C1' then 1
when @input = 'client2' and clientid in ( 'C2', 'C3', 'C4' ) then 1
else 0 end = 1;
这可能会受到影响。问题是试图在非表变量的变量中保存多个值。使用表变量并将其与联接一起使用。问题是试图在非表变量的变量中保存多个值。使用一个表变量并将其与联接一起使用。这已经得到了完美的解释。非常感谢你的帮助!这是很好的解释。非常感谢你的帮助!