Solved

IN with @variable

Posted on 2016-11-28
5
28 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 46
How to search for strings inside db views 4 39
TSQL recursive CTE challenge... 8 34
What can the "OPTION" in the GROUP help me in my query? 4 35
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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