SQL Server - Multi-Parameter Stored Procedure help

Asked By OriginalStealth
19-Jan-10 08:09 AM
stored procedure 1:  exec getcolor @color = blue....this runs fine.
stored procedure 2:  exec getcolors @colors in('red', 'white',
'blue').........bombs

code is

create procedure getcolors  @colors nvarchar (50)
as
(select *
from hrs_by_activity
where  color in(@colors)
)

need help making this work.
thanks in advance
SQL Server
(1)
Stored procedure
(1)
CREATE FUNCTION
(1)
Nvarchar
(1)
Smallint
(1)
NOT NULL
(1)
Varchar
(1)
Nchar
(1)
  Bruce L-C [MVP] replied to OriginalStealth
19-Jan-10 02:02 PM
Below is my standard response about this. As you will see, it touches
exactly on what you are doing and why it does not work and what you need to
do:

What does not work has nothing really to do with RS but has to do with Stored
Procedures in SQL Server. You cannot do the following in a stored procedure.

Let's say you have a Parameter called @MyParams

Now you can map that parameter to a multi-value parameter but if in your
stored procedure you try to do this:

select * from sometable where somefield in (@MyParams)

It will not work. Try it. Create a stored procedure and try to pass a
multi-value parameter to the stored procedure. It will not work.

What you can do is to have a string parameter that is passed as a multivalue
parameter and then change the string into a table.

This technique was told to me by SQL Server MVP, Erland Sommarskog

For example I have done this

inner join charlist_to_table(@STO,Default)f on b.sto = f.str

So note this is NOT an issue with RS, it is strictly a stored procedure
issue.

Here is the function:
CREATE FUNCTION charlist_to_table
(@list      ntext,
@delimiter nchar(1) = N',')
RETURNS @tbl TABLE (listpos int IDENTITY(1, 1) NOT NULL,
str     varchar(4000),
nstr    nvarchar(2000)) AS

BEGIN
DECLARE @pos      int,
@textpos  int,
@chunklen smallint,
@tmpstr   nvarchar(4000),
@leftover nvarchar(4000),
@tmpval   nvarchar(4000)

SET @textpos = 1
SET @leftover = ''
WHILE @textpos <= datalength(@list) / 2
BEGIN
SET @chunklen = 4000 - datalength(@leftover) / 2
SET @tmpstr = @leftover + substring(@list, @textpos, @chunklen)
SET @textpos = @textpos + @chunklen

SET @pos = charindex(@delimiter, @tmpstr)

WHILE @pos > 0
BEGIN
SET @tmpval = ltrim(rtrim(left(@tmpstr, @pos - 1)))
INSERT @tbl (str, nstr) VALUES(@tmpval, @tmpval)
SET @tmpstr = substring(@tmpstr, @pos + 1, len(@tmpstr))
SET @pos = charindex(@delimiter, @tmpstr)
END

SET @leftover = @tmpstr
END

INSERT @tbl(str, nstr) VALUES (ltrim(rtrim(@leftover)),
ltrim(rtrim(@leftover)))
RETURN
END

GO


--
Bruce Loehle-Conger
MVP SQL Server Reporting Services
  Kathi Knobloch replied to Bruce L-C [MVP]
22-Feb-10 02:01 PM
I am using the function below to create an array to pass into my Stored Procedure for a SSRS report and it has been working wonderfully. But today I ran across a situation where it appears that it is unable to return everything to me. I have a SQL table that is a "remarks/comments" table - this table includes a Student_ID, Student_Name, Remarks_Type, Remarks_Author_ID, Remarks_Author_Name, Remarks.  I'm needing to allow user to select(Multi-Value) Author_ID & (Multi-Value) Remarks_Type for my parameters.  In my where clause of my standard select statment if I use the following, it works just fine:

WHERE Author_ID IN ('00064', '00089', '00099', '00118')

and Remarks_Type IN ('TAX', 'TCXX', 'FFAC')

But when I try the following in my Stored Procdure, it only returns a handful of records:

WHERE Author_ID IN (SELECT nstr FROM dbo.fn_Charlist_to_tbl (@strAuthor,','))

and Remarks_Type IN (SELECT nstr FROM dbo.fn_Charlist_to_tbl (@strType,','))



My best guess is that I'm returning too much data to be handle with this function?? I have 4 distinct Author_ID(s) and about 10 distinct Remarks_Type  -  but many of the actual "Remarks" for a person that are returned are pretty lengthy...Any thoughts of what might be going on here or how to fix this?
  Kathi Knobloch replied to Kathi Knobloch
22-Feb-10 02:18 PM
Upon further testing, the above isn't happening, I don't know what is going on exactly, but ignore above comment.
help
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit seconds left to the installation and bombed with the same errors as the Crystal Reports Server install. The error is the same whether I try to install MSDE by itself or
SQL Express: Failed Reinstall SQL Server Dear Setup Experts: I am trying to reinstall SQL Express after playing with it some. I uninstalled it, and now, it refuses to reinstall in the face. Any ideas what this log file means and how I can get SQL Express installed? I really do not want to have to reinstall my whole system. * ** ** Start of Log File Overall summary: Final result: SQL Server installation failed. To continue, investigate the reason for the failure, correct the problem, uninstall SQL Server, and then rerun SQL Server Setup. Exit code (Decimal): -2068643839 Exit facility code: 1203
Moving database decrease performance SQL Server I have moved database from one SQL2000 on win server to another standalone computer. Both SQL serverers have the same settings, same service pack, enterprise editions. On both SQL servers are tempDB, log file and mdf file on separate disks. The new SQL server is on better computer, has more RAM, beter disks, stronger proc, but all queries are there some guide for this scenario somewhere on the net? Any suggestions? Thank you, Simon SQL Server Programming Discussions SQL Server 2008 (1) SQL Server 2000 (1) SQL Server Books (1
SQLServer Error: 802, 701, 733 SQL Server SQL Server 2008 on Win2008 R2 x64 Server, 8 GB RAM - running virtualized on VMWare ES-Server Every sunday some maintainance jobs are scheduled to run. Rebuilding indices and statistics fail with There is insufficient memory available in the buffer pool. [SQLSTATE 42000] (SaveAllSchedules) After this the server is no longer reachable: [298] SQLServer Error: 773, SQL Server Network Interfaces: The requested security package does not exist [SQLSTATE 08001] . . . [298] SQLServer Error
Correct syntax please SQL Server Hello, I am building a Windows Forms App with VB.NET using SQL Server 2008 R2. I have got a search form where users can search for records from the db. The App calls a stored procedure which executes the sql below. The form has two search criteria users have requested, 'labno' and 'DateAssayReq'. The App labno exists in the db. Can someone see what I have done wrong? TIA. @labno nvarchar(6) = NULL, @DateAssayReq date AS SELECT assayid, DateAssayReq, LabNo, ReasonRepeat, PersonReq, RepeatedYN, DateRepeated, DtRepeatDy, PersonRepeat