Solved

IN with @variable

Posted on 2016-11-28
5
14 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
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now