Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sp_executesql and OUTPUT parameters

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-27 : 12:14:52
Afternoon all

How do I output the result of the following dynamic sql statement?:


CREATE PROCEDURE [dbo].[usp_COUNTDocumentSearchResults]
(
@clientID INT
, @accountNumber VARCHAR(15) = NULL
, @invoiceNumber VARCHAR(20) = NULL
, @firstDate DATETIME = NULL
, @secondDate DATETIME = NULL
, @handsetNumber VARCHAR(12) = NULL
, @recipientName VARCHAR(45) = NULL
, @count INT OUTPUT
)
AS
SET NOCOUNT ON

SET @handsetNumber = REPLACE(@handsetNumber, ' ', '')

DECLARE @sql NVARCHAR(4000)

SET @sql = 'SELECT COUNT(DS.DocumentSetID)
FROM Client C
INNER JOIN Account A
ON (
(C.ClientID = A.ClientID)
AND (A.ClientID = ' + CONVERT(VARCHAR,@clientID) + ')'

IF @accountNumber IS NOT NULL
BEGIN
IF CHARINDEX('*',@accountNumber) = 0
BEGIN
SET @sql = @sql + ' AND (AccountNumber = ''' + @accountNumber + ''')'
END
ELSE IF CHARINDEX('*',@accountNumber) = 1
BEGIN
SET @sql = @sql + ' AND (AccountNumber LIKE ''%' + REPLACE(@accountNumber,'*','') + ''')'
END
ELSE
BEGIN
SET @sql = @sql + ' AND (AccountNumber LIKE ''' + REPLACE(@accountNumber,'*','') + '%'')'
END
END

SET @sql = @sql + ' )'

SET @sql = @sql + ' INNER JOIN DocumentRecipient DR
ON (
(A.AccountID = DR.AccountID)'

IF @recipientName IS NOT NULL
BEGIN
SET @sql = @sql + ' AND (DocumentRecipientName LIKE (''%' + @recipientName + '%''))'
END

SET @sql = @sql + ' )'


SET @sql = @sql + ' INNER JOIN Document D
ON (
(DR.DocumentRecipientID = D.DocumentRecipientID)'

IF @invoiceNumber IS NOT NULL
BEGIN
SET @sql = @sql + ' AND (DocumentInvoiceNumber = '''+ @invoiceNumber + ''')'
END


IF (@firstDate IS NOT NULL)
BEGIN
IF @secondDate IS NULL
BEGIN
SET @sql = @sql + 'AND (DocumentDate = ''' + CONVERT(VARCHAR,@firstDate) + '''' + ')'
END
ELSE IF @secondDate IS NOT NULL
BEGIN
SET @sql = @sql + 'AND (DocumentDate BETWEEN ''' + CONVERT(VARCHAR,@firstDate) + '''' + ' AND ' + '''' + CONVERT(VARCHAR,@secondDate) + '''' + ')'
END
END

SET @sql = @sql + ' )'

IF @handsetNumber IS NOT NULL
BEGIN
SET @sql = @sql + ' INNER JOIN Handset H
ON (
D.DocumentID = H.DocumentID
AND (HandsetNumber = ''' + @handsetNumber + '''' + ')
)'
END

SET @sql = @sql + ' INNER JOIN DocumentSet DS ON DS.DocumentSetID = D.DocumentSetID
INNER JOIN [File] F ON F.FileID = DS.FileID
INNER JOIN Status S ON (S.StatusID = F.StatusID AND StatusName = ''Accessible'')'


EXEC sp_executesql @sql
GO


Thanks in advance

Hearty head pats

Kristen
Test

22859 Posts

Posted - 2006-02-27 : 13:31:44
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=41413&SearchTerms=sp_executesql,output

Kristen
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-28 : 06:33:44
Thanks Kristen, that now works great!

Hearty head pats
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-28 : 09:24:05
I have another question regarding sp_executesql........... Can you use a paramatised TOP clause like so:


SELECT TOP @limitVal DS.DocumentSetID
, D.DocumentDate
, A.AccountNumber
FROM Client C
INNER JOIN Account A .......etc


At the moment, I am doing this:


SELECT TOP ' + CONVERT(VARCHAR,@limit) + ' DS.DocumentSetID
, D.DocumentDate
, A.AccountNumber..................


The bottom method works fine, but I am using sp_executesql and therefore, as embeding parameters in the dynamic sql string. But if I try the top method I get teh following error method:

Server: Msg 170, Level 15, State 1, Line 18
Line 18: Incorrect syntax near '@limitVal'.
Stored Procedure: Test_ArchiveIndex.dbo.usp_SEARCHDocuments_v2_1
Return Code = 0

????

Hearty head pats
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-02-28 : 09:48:34
Yes, you could use sp_executeSQL

You could also use

SET ROWCOUNT @limitVal

SELECT TOP @limitVal DS.DocumentSetID
, D.DocumentDate
, A.AccountNumber
FROM Client C
INNER JOIN Account A .......etc
ORDER BY ... sequence columns ...

SET ROWCOUNT 0 -- Reset to All Rows - VERY important!

Note that whilst SET ROWCOUNT is in effect ALL operations (in your session) are restricted to that limit - including UPDATES and Nested Sub SELECTS [I think ...]

Kristen
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-28 : 10:03:39
Thanks again Kristen!

Hearty head pats
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-03-01 : 11:46:48
I have another question regarding sp_executesql:

Can you execute a stored procedure dynamically? Like so:

DECLARE @clientID INT
DECLARE @sprocName VARCHAR(40)
DECLARE @sql NVARCHAR(1000)
DECLARE @params NVARCHAR(1000)

SET @clientID = 1
SET @sprocName = 'dbo.usp_SEARCHDocuments_v2'

SET @sql = 'EXEC @sprocNameVal @clientIDVal,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL'

SET @params = N'@sprocNameVal VARCHAR(100), @clientIDVal INT'

EXEC sp_executesql @sql, @params, @sprocNameVal = @sprocName, @clientIDVal = @clientID

At the moment, although it appears to run, I don't receive any results, so what am I doing wrong (and is this possible in the first place?)?

Thanks

Hearty head pats
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-03-01 : 12:11:31
Doh, my bad! All sorted now!

Hearty head pats
Go to Top of Page
   

- Advertisement -