Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning results from dynamic query 2

Status
Not open for further replies.
Jan 8, 2007
8
US
Hopefully this is a simple question and I'm just not seeing the answer sitting in front of my face...

I'm trying to return the results of a dynamic query to the batch, and so far I've had trouble.

Here's an excerpt:
Code:
Declare @QueryPending nvarchar(4000)

Set @QueryPending = 
 N' Select *
      From STop_' + @dbDataStream + '.dbo.AGTXREF
      Where CUST# = ' + Convert(varchar(8), @CUST#) + '
        And AGTSTS = ''C''
        And AGSSTS = ''NA''
 '

-- This next line is what I'd like to accomplish, in some form or another
If Exists ( Execute sp_executesql @QueryPending ) Set @Status = 'P'

Any ideas?
 
Insert the results into a temp table (define it first, don;t use select into) and then select from the temp table in the If Exists.

Questions about posting. See faq183-874
 
Use an output parameter, example in pubs db

Code:
USE pubs
GO


DECLARE @tbl    nvarchar(50),
        @sql    nvarchar(4000),
        @params nvarchar(4000),
        @count  int
SELECT @tbl =N'authors'

   SELECT @sql =
   N' SELECT @cnt = COUNT(*) FROM dbo.' + quotename(@tbl) 
    SELECT @params =  N'@cnt      int      OUTPUT'

   EXEC sp_executesql @sql, @params, @cnt = @count OUTPUT

SELECT @count

Denis The SQL Menace
SQL blog:
 
Other way:
Code:
SET NOCOUNT ON
Declare @QueryPending nvarchar(4000)

Set @QueryPending =
 N' Select SmallestFieldInTheTable
      From STop_' + @dbDataStream + '.dbo.AGTXREF
      Where CUST# = ' + Convert(varchar(8), @CUST#) + '
        And AGTSTS = ''C''
        And AGSSTS = ''NA''
 '
DECLARE @temp TABLE (SmallestFieldInTheTable TypeOfThatField)
INSERT INTO @Temp
EXEC (@test)
IF @@ROWCOUNT <> 0
   Set @Status = 'P'

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Thanks to both, I like the solution SQLDenis came up with and I'm going to go that route, but SQLSister's proposal was a good idea as well.

Thanks again!
 
Why not
Code:
Execute sp_executesql @QueryPending
If @@ROWCOUNT > 0  Set @Status = 'P'
 
yep you can do that for this query however if you do a count result will always be 1


exec works also for this purpose

example
Code:
Execute sp_executesql N'select 1 union all select 2'
select  @@ROWCOUNT 


exec ('select 1 union all select 2')
select  @@ROWCOUNT

but this won't work

Code:
use pubs

Execute sp_executesql N'select count(*) from authors'
select  @@ROWCOUNT 

Execute sp_executesql N'select count(*) from authors where 1 =2'
select  @@ROWCOUNT

Denis The SQL Menace
SQL blog:
 
The side effect of direct using of the query is that you will get a resultset. But if you want only to check if the record exists I thing that is not the way :) In the original post the desire was:
Code:
IF EXISTS(....)
   do something
(I prefer Denis's solution)

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top