Solved

IN with @variable

Posted on 2016-11-28
5
27 Views
Last Modified: 2016-11-28
Hi experts

this is working
declare @user_panama nvarchar(55) = 'MSANCHEZ';

this is NOT working
declare @user_panama nvarchar(55) = 'MSANCHEZ' + ',' + 'VDIAZ';

this is the predicate
  where
  [Usuarios] IN (@user_panama)
0
Comment
Question by:enrique_aeo
  • 3
5 Comments
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 41904251
Hi enrique_aeo,

Create a below function to your database and pass the parameter to it.


CREATE FUNCTION [dbo].[CSVToTable] (@InStr VARCHAR(MAX))

RETURNS @TempTab TABLE

   (SSNSoftwareSerialNumber Varchar(50) not null)

AS

BEGIN

    ;-- Ensure input ends with comma

	SET @InStr = REPLACE(@InStr + ',', ',,', ',')

	DECLARE @SP INT

DECLARE @VALUE VARCHAR(1000)

WHILE PATINDEX('%,%', @INSTR ) <> 0 

BEGIN

   SELECT  @SP = PATINDEX('%,%',@INSTR)

   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)

   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')

   INSERT INTO @TempTab(SSNSoftwareSerialNumber) VALUES (@VALUE)

END

	RETURN

END

Open in new window



SELECT SSNSoftwareSerialNumber FROM dbo.CSVToTable(@user_panama)

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41904254
you won't be able to do it like this. You will need to convert your variable into a table. Have a look at http://emoreau.com/Entries/Blogs/2015/04/Just-learned-a-nifty-t-SQL-trick-this-morning.aspx for an example
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 41904258
this is NOT working:
 declare @user_panama nvarchar(55) = 'MSANCHEZ' + ',' + 'VDIAZ';

It will only work if you pass the parameter like this 'MSANCHEZ','VDIAZ'  not like MSANCHEZ,VDIAZ

declare @user_panama nvarchar(55) = '''MSANCHEZ''' + ',' + '''VDIAZ''';
select @user_panama;
0
 

Author Comment

by:enrique_aeo
ID: 41904289
this is the data
SAP_BC_BASIS_ADMIN_PG_PA      ATI
SAP_BC_BMT_WFM_ADMIN_PG_PA      ICHANG
SAP_BC_BMT_WFM_ADMIN_PG_PA      JARENAS
SAP_BC_BMT_WFM_ADMIN_PG_PA      JPILCO
SAP_BC_BMT_WFM_ADMIN_PG_PA      LENCISO

go
declare @user_panama nvarchar(55) = '''ATI''' + ',' + '''ICHANG''';
SELECT distinct [Rol] + '_PG_PA' as 'ROL_PANAMA', [Usuarios]
FROM [AdventureWorks].[dbo].[usuarios_para_panama]
where
            [Usuarios] IN (@user_panama)

ORDER BY 1

this is the results
(0 row(s) affected)
schema_data_user_pn---copia.sql
0
 
LVL 21

Accepted Solution

by:
Tapan Pattanaik earned 500 total points
ID: 41904326
Hi Enrique,

User my below function  "CSVToTable" and pass the parameter to it.

CREATE FUNCTION [dbo].[CSVToTable] (@InStr VARCHAR(MAX))

RETURNS @TempTab TABLE

   (SSNSoftwareSerialNumber Varchar(50) not null)

AS

BEGIN

    ;-- Ensure input ends with comma

	SET @InStr = REPLACE(@InStr + ',', ',,', ',')

	DECLARE @SP INT

DECLARE @VALUE VARCHAR(1000)

WHILE PATINDEX('%,%', @INSTR ) <> 0 

BEGIN

   SELECT  @SP = PATINDEX('%,%',@INSTR)

   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)

   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')

   INSERT INTO @TempTab(SSNSoftwareSerialNumber) VALUES (@VALUE)

END

	RETURN

END

Open in new window




declare @user_panama nvarchar(55) = 'ATI' + ',' + 'ICHANG';
 
SELECT  distinct [Rol] + '_PG_PA' as 'ROL_PANAMA', [Usuarios]
 FROM [AdventureWorks].[dbo].[usuarios_para_panama]
 where [Usuarios] in (SELECT SSNSoftwareSerialNumber FROM dbo.CSVToTable(@user_panama))

Open in new window

SqlQuery-Screen-shot.PNG
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question