Showing posts with label parameter. Show all posts
Showing posts with label parameter. Show all posts

Sunday, March 25, 2012

CURSOR AS PROCEDURE OUTPUT

I've written this GenericCursor procedure that return a CURSOR as output parameter, where the CURSOR is opened by a dynamic SQL statement executed via sp_executesql:

CREATE PROCEDURE dbo.GenericCursor
@.genericCursor CURSOR VARYING OUTPUT
, @.CMD Nvarchar(1024)
AS
BEGIN
DECLARE @.CMDx Nvarchar(1024);
SET @.CMDx = 'SET @.genericCursor = CURSOR FORWARD_ONLY STATIC FOR ' + @.CMD + '; OPEN @.genericCursor;'
exec sp_executesql @.CMD,
N'@.utentiCursor CURSOR out',
@.genericCursor out
END

Everityng works fine: I want to use GenericCursor procedure and it works, but when I try to CLOSE and then DEALLOCATE the CURSOR it raises an error (as in the example):

CREATE PROCEDURE testGenericCursor
AS
BEGIN
DECLARE @.MyCursor CURSOR;
DECLARE @.CMDx Nvarchar(1024);
SET @.CMDx = 'SELECT idUtente FROM Utenti'
EXEC dbo.UtentiCursor @.MyCursor OUTPUT, @.CMDx;
WHILE (@.@.FETCH_STATUS = 0)
BEGIN;
FETCH NEXT FROM @.MyCursor;
END;
-- CLOSE @.MyCursor;
-- DEALLOCATE @.MyCursor;
END


Why CLOSE and DEALLOCATEare not usable? They generate the following errors:

Msg 16950, Level 16, State 2, Line 13
The variable '@.MyCursor' does not currently have a cursor allocated to it.
Msg 16950, Level 16, State 2, Line 14
The variable '@.MyCursor' does not currently have a cursor allocated to it.

I don't think you ever put the cursor into your output parameter in the first place (unless you just made a copy\paste error when putting your code into the post).

The problem is here:

DECLARE @.CMDx Nvarchar(1024);
SET @.CMDx = 'SET @.genericCursor = CURSOR FORWARD_ONLY STATIC FOR ' + @.CMD + '; OPEN @.genericCursor;' <<<< This won't work (see below)
exec sp_executesql @.CMD, <<<< Shouldn't this be @.CMDx?

Your going to have to pass @.genericCursor into sp_executesql as an output parameter as well, something like this:

DECLARE @.CMDx Nvarchar(1024);
SET @.CMDx = 'SET @.genericCursor = CURSOR FORWARD_ONLY STATIC FOR ' + @.CMD + '; OPEN @.genericCursor;'
exec sp_executesql @.CMDx, N'@.genericCursor cursor varying output', @.genericCursor output

|||

The CLOSE and DEALLOCATE are in a different scope than the cursor...they don't know each other exist.

Your code revised:

CREATE PROCEDURE dbo.GenericCursor

@.genericCursor CURSOR VARYING OUTPUT

, @.CMD Nvarchar(1024)

AS

BEGIN

DECLARE @.CMDx Nvarchar(1024);

SET @.CMDx = 'SET @.genericCursor = CURSOR FORWARD_ONLY STATIC FOR ' + @.CMD + '; OPEN @.genericCursor;'

exec sp_executesql @.CMD,

N'@.utentiCursor CURSOR out',

@.genericCursor out

CLOSE @.genericCursor

DEALLOCATE @.genericCursor

END

CREATE PROCEDURE testGenericCursor

AS

BEGIN

DECLARE @.MyCursor CURSOR;

DECLARE @.CMDx Nvarchar(1024);

SET @.CMDx = 'SELECT name FROM dbo.sysobjects;'

EXEC dbo.GenericCursor @.MyCursor OUTPUT, @.CMDx;

WHILE (@.@.FETCH_STATUS = 0)

BEGIN;

FETCH NEXT FROM @.MyCursor;

END

END

EXECdbo.testGenericCursor

This is an example from the EXECUTE (Transact-SQL) topic in Books Online that uses EXEC:

http://msdn2.microsoft.com/en-us/library/ms188332.aspx

USE AdventureWorks;

GO

DECLARE tables_cursor CURSOR

FOR

SELECT s.name, t.name

FROM sys.objects AS t

JOIN sys.schemas AS s ON s.schema_id = t.schema_id

WHERE t.type = 'U';

OPEN tables_cursor;

DECLARE @.schemaname sysname;

DECLARE @.tablename sysname;

FETCH NEXT FROM tables_cursor INTO @.schemaname, @.tablename;

WHILE (@.@.FETCH_STATUS <> -1)

BEGIN;

EXEC ('ALTER INDEX ALL ON ' + @.schemaname + '.' + @.tablename + ' REBUILD;');

FETCH NEXT FROM tables_cursor INTO @.schemaname, @.tablename;

END;

PRINT 'The indexes on all tables have been rebuilt.';

CLOSE tables_cursor;

DEALLOCATE tables_cursor;

GO

A good link for dynamic SQL:

http://www.sommarskog.se/dynamic_sql.html#cursor0

|||He is using cursor variables, which can be passed between scopes just like he is doing in dbo.GenericCursor. He just has to modify his sp_executesql call to pass the cursor variable as an output parameter.|||

Thanks to evryone out there: I noticed I've posted the wrong code :-P

> SO THIS MEANS YOU ARE SO GOOD THAT YOU'VE UNDERSTOOD IT EVEN IF IT WAS WRONG!!!

Just to summarize what the matter is, this is the final working version:

CREATE PROCEDURE dbo.GenericCursor

@.genericCursor CURSOR VARYING OUTPUT

, @.CMD Nvarchar(1024)

AS

BEGIN

DECLARE @.CMDx Nvarchar(1024);

SET @.CMDx = 'SET @.genericCursor = CURSOR FORWARD_ONLY STATIC FOR ' + @.CMD + '; OPEN @.genericCursor;'

exec sp_executesql @.CMDx,

N'@.genericCursor cursor output',

@.genericCursor out

END

--

CREATE PROCEDURE testGenericCursor

AS

BEGIN

DECLARE @.MyCursor CURSOR;

DECLARE @.name as varchar(100);

DECLARE @.CMDx Nvarchar(1024);

SET @.CMDx = 'SELECT TOP 50 name FROM dbo.sysobjects;'

EXEC dbo.GenericCursor @.MyCursor OUTPUT, @.CMDx;

FETCH NEXT FROM @.MyCursor INTO @.name;

WHILE (@.@.FETCH_STATUS = 0)

BEGIN;

FETCH NEXT FROM @.MyCursor INTO @.name;

SELECT @.name

END

CLOSE @.MyCursor

DEALLOCATE @.MyCursor

END

--

EXEC dbo.testGenericCursor

Sunday, February 19, 2012

CSV string as a SQL In Parameter

Hi Guys,

I am having SQL query whith "IN"

SELECT * FROM Table1 Where ID in ( 1, 2,3)

how can i pass '1,2,3' as a SP parameter.

Thanks

There is a simple way to resolve this
exec ('select *from tblwhere id in (' + @.csv + ')' )
 
where @.csv is your parameter with comma sparated value.
might this helps you.
 
Thanks
 
|||
Hi, 
There is a simple way to resolve this
exec ('select * from tbl where id in ( ' + @.csv + ')' )
 
where @.csv is your parameter with comma sparated value.
might this helps you.
 
Thanks

Friday, February 17, 2012

CSV Field Stored Procedure

Hi All,
I would like to submit a large amount of CSV text into a stored procedure. At present I'm using a varchar(8000) parameter and then converting this into a temporary table using something like the sql below. My question is does anybody have any suggestions on how to get round the 8000 limit without doing round trips?
thanks
Steve
declare @.separator char(3)
set @.separator = '%' + @.delimeter + '%'
declare @.separator_position int
declare @.array_value varchar(1000)
set @.input = @.input + ','
while patindex(@.separator , @.input) <> 0
begin
select @.separator_position = patindex(@.separator , @.input)
select @.array_value = left(@.input, @.separator_position - 1)
Insert @.IntTable
Values (@.array_value)
select @.input = stuff(@.input, 1, @.separator_position, '')
end
Multiple parameters?
--=20
Keith
"Steve" <steve@.nospam.com> wrote in message =
news:empTbYQVEHA.1164@.tk2msftngp13.phx.gbl...
Hi All,
I would like to submit a large amount of CSV text into a =
stored procedure. At present I'm using a varchar(8000) parameter and =
then converting this into a temporary table using something like the sql =
below. My question is does anybody have any suggestions on how to get =
round the 8000 limit without doing round trips?
thanks
Steve
declare @.separator char(3)
set @.separator =3D '%' + @.delimeter + '%'
declare @.separator_position int=20
declare @.array_value varchar(1000)=20
=20
set @.input =3D @.input + ','
=20
while patindex(@.separator , @.input) <> 0=20
begin
=20
select @.separator_position =3D patindex(@.separator , @.input)
select @.array_value =3D left(@.input, @.separator_position - 1)
=20
Insert @.IntTable
Values (@.array_value)
select @.input =3D stuff(@.input, 1, @.separator_position, '')
end
|||Steve,
You can pass the CSV text as a parameter of type text or ntext, and use PATINDEX and SUBSTRING to parse it. An alternative, that if possible will make parsing the long parameter much easier is to pass it as non-separated text with a fixed-length field width instead of comma-separated. You can find an example of this technique at http://www.sommarskog.se/arrays-in-s...xstring_multi, and the entire article http://www.sommarskog.se/arrays-in-sql.html may also be useful.
Steve Kass
Drew University
"Steve" <steve@.nospam.com> wrote in message news:empTbYQVEHA.1164@.tk2msftngp13.phx.gbl...
Hi All,
I would like to submit a large amount of CSV text into a stored procedure. At present I'm using a varchar(8000) parameter and then converting this into a temporary table using something like the sql below. My question is does anybody have any suggestions on how to get round the 8000 limit without doing round trips?
thanks
Steve
declare @.separator char(3)
set @.separator = '%' + @.delimeter + '%'
declare @.separator_position int
declare @.array_value varchar(1000)
set @.input = @.input + ','
while patindex(@.separator , @.input) <> 0
begin
select @.separator_position = patindex(@.separator , @.input)
select @.array_value = left(@.input, @.separator_position - 1)
Insert @.IntTable
Values (@.array_value)
select @.input = stuff(@.input, 1, @.separator_position, '')
end
|||Many thanks for your help guys.
Regards
Steve
"Steve" <steve@.nospam.com> wrote in message news:empTbYQVEHA.1164@.tk2msftngp13.phx.gbl...
Hi All,
I would like to submit a large amount of CSV text into a stored procedure. At present I'm using a varchar(8000) parameter and then converting this into a temporary table using something like the sql below. My question is does anybody have any suggestions on how to get round the 8000 limit without doing round trips?
thanks
Steve
declare @.separator char(3)
set @.separator = '%' + @.delimeter + '%'
declare @.separator_position int
declare @.array_value varchar(1000)
set @.input = @.input + ','
while patindex(@.separator , @.input) <> 0
begin
select @.separator_position = patindex(@.separator , @.input)
select @.array_value = left(@.input, @.separator_position - 1)
Insert @.IntTable
Values (@.array_value)
select @.input = stuff(@.input, 1, @.separator_position, '')
end

Tuesday, February 14, 2012

Crytarl Report with Dabase access

dear all ,

i use crystal report from dot net 2003 (i use dotnet version architect).
with database access.

and i have problem to set parameter for query access in crystal report.

if i use database mssqlserver 2000 in crystal report i just select store procedure and the parameter for store precedure will otomatically add in crystal report.

sampel query store procedure like this

select * from tblcustomer
where CustName = @.custName

the parameter @.CustName otomatically add in crystal report.

how to implament that if i use database access.
so i can send a value for @.Custname from my applicationAs i have worked for VB.net so i'll how i used to send parameter value externally
first u need to define
Dim pv As New CrystalDecisions.Shared.ParameterValues
Dim pdv As New CrystalDecisions.Shared.ParameterDiscreteValue

pdv.Value = EntityCode
pv.Add(pdvScriptCode1)

RptViewer.DataDefinition.ParameterFields("@.Customer").ApplyCurrentValues(pv)

thats it...
Try it out n tell me the result whther its working......