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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function with dinamic query returning a value

Status
Not open for further replies.

camaleonCHILE

IS-IT--Management
Jul 7, 2005
20
0
0
Hi dear friends !!!

I need obtain a return value from a function. In this function, I work with a query built dynamically, from which I get the value. So the thing is:

DECLARE @X INTEGER
DECLARE @STRING NVARCHAR(4000)

SET @STRING='SELECT COUNT(*) FROM MY_TABLE'

EXECUTE(@STRING)

i need to put the count(*) value in the @X variable and return it.

Thanks in advance





 
Considering above example... why dynamic?

------
[small]<this is sig>
select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')
</this is sig>[/small]
[banghead]
 
because I've to add some business rules to that query,
something like this:


SET @STRING='SELECT COUNT(*) FROM MY_TABLE '

IF @PARAMETER=1
SET @STRING=@STRING + 'WHERE FIELD1 IS NULL'
ELSE
SET @STRING=@STRING + 'WHERE FIELD1 IS NOT NULL'


thanks for answer me

Regards



 
Is that the extent of the business rule? Are there more rules?

If that's it, then you could do this...

Code:
Declare @AllCount Integer
Declare @NullCount Integer
Declare @NotNullCount Integer

Select  @AllCount = Count(1),
        @NotNullCount = Count(Field1),
        @NullCount = Count(1) - Count(Field1)
From    MY_TABLE

If @PARAMETER=1
    Set @X = @NullCount
Else
    Set @X = @NotNullCount

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top