Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using IN() and parameter
Message
From
07/02/2013 19:03:24
 
 
To
07/02/2013 16:10:56
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01565428
Message ID:
01565448
Views:
46
>Hi,
>
>I have a stored procedure defined like this:
>
>
ALTER PROCEDURE [dbo].[AirWayBillsSelectByManifestNumbers]
>(
>	@awb_manifestnumber varchar(100)
>)
>AS
>	SET NOCOUNT ON;
>
>SELECT [awb_pk]
>      ,[awb_keycode]
>      ,[awb_manifestdate]
>      ,[awb_manifestnumber]
>      ,[awb_pieces]
>      ,[awb_weight]
>      ,[awb_cusfk]
>      ,[awb_value]
>      ,[awb_freight]
>      ,[awb_fuel]
>      ,[awb_insurance]
>      ,[awb_number]
>      ,[awb_shipper]
>      ,[awb_description]
>      ,[cus_number]
>  FROM [dbo].[AirWayBills]
>  inner join [dbo].[Customers] on awb_cusfk = cus_PK
>	WHERE 
>		([awb_manifestnumber] IN (@awb_manifestnumber))
>
>awb_manifestnumber is an int.
>
>This works fine if I send in one manifestnumber. However if I send in two like this: '12345,98765' I get an error that the string can't be converted to int. Which of course makes sense.
>
>I have tried a function to split the string into a table with integers and joining this table, but this is too slow and the reportviewer that I am using times out (apparently).
>
>This is the function I tried:
>
>http://www.codeproject.com/Articles/7938/SQL-User-Defined-Function-to-Parse-a-Delimited-Str
>
>and my SP was:
>
>
ALTER PROCEDURE [dbo].[AirWayBillsSelectByManifestNumbers]
>(
>	@awb_manifestnumber varchar(100)
>)
>AS
>	SET NOCOUNT ON;
>
>if object_id('tepmdb..#awblist',N'U') IS NOT NULL drop table #awblist;
>
>select int_value
>	into #awblist
>	from dbo.fn_ParseText2Table(@awb_manifestnumber, ',')
>
>SELECT [awb_pk]
>      ,[awb_keycode]
>      ,[awb_manifestdate]
>      ,[awb_manifestnumber]
>      ,[awb_pieces]
>      ,[awb_weight]
>      ,[awb_cusfk]
>      ,[awb_value]
>      ,[awb_freight]
>      ,[awb_fuel]
>      ,[awb_insurance]
>      ,[awb_number]
>      ,[awb_shipper]
>      ,[awb_description]
>      ,[cus_number]
>  FROM [dbo].[AirWayBills]
>  inner join [dbo].[Customers] on awb_cusfk = cus_PK
>  inner join #awblist on #awblist.int_value = awb_manifestnumber
>
>Any suggestions for alternatives or techniques to speed this up?
>
>In case it makes a difference, I am calling this from a C# WPF application and I build up the list of manifest numbers from a datagrid.


I created a user function to handle this.
CREATE FUNCTION [dbo].[isIN] 
(
	@lcStr varchar(100) ,
	@lcList varchar(max)
)
RETURNS int 
AS
BEGIN

	DECLARE @Test int = 0

	if @lcStr = '' or @lcList=''
		set @test=1 
	else 
	if NOT (@lcStr is null or @lcList is null)
	begin 
		declare @aa int 
		set @aa = charindex(','+@lcStr+',',','+@lcList+',')
		if @aa>0
			set @Test = 1
		else 
			set @test = 0
	end 

	RETURN @Test

END
GO
then, in the conditional clause I would
WHERE dbo.isIN([awb_manifestnumber], @awb_manifestnumber)>0
Greg Reichert
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform