1

I'm using SQL Server 2005, and want a user defined function to implement the following:

Parameter: @SQL varchar(max)

execute 'select count(1) from (' + @sql + ')'

and return the result as integer.

This is the dummy code >>

ALTER FUNCTION [dbo].udf_GetCountFromSQL ( @SQL VARCHAR(MAX) )
RETURNS INT
AS 
    BEGIN
        DECLARE @returnValue INT
        SET @SQL = 'SELECT COUNT(1) FROM (' + @SQL + ')'
        EXEC @returnValue= @SQL

        RETURN @returnValue ;
    END

@SQL here is dynamic sql, not stored procedure name, so it doesn't work.

Wait to be helped out, thanks in advance.

Elaine

3
  • @Pavanred, is come up with the function that can get the count(..) , I'm looking at jimbojw.com/wiki/…, looks like it's impossible to use dynamical sql inside a UDF. Commented Dec 23, 2010 at 10:14
  • Why COUNT(1) instead of COUNT(*) ? Commented Dec 23, 2010 at 10:30
  • @iDevlop both works, doesn't matter :) Commented Dec 24, 2010 at 1:21

3 Answers 3

4

You can't execute dynamic SQL in a user defined function. You'd need to change it to a stored procedure and use sp_executesql to get the count into a variable - you can then pass the count out as an OUTPUT parameter (or RETURN value if you want).

CREATE PROCEDURE dbo.ExampleSproc(@SQL NVARCHAR(MAX), @RowCount INTEGER OUTPUT)
AS
BEGIN
    SET @SQL = 'SELECT @RowCount = COUNT(1) FROM (' + @SQL + ') x'
    EXECUTE sp_executesql @SQL, N'@RowCount INTEGER OUT', @RowCount OUT
END

Then call like this:

DECLARE @RowCount INTEGER
EXECUTE dbo.ExampleSproc '...Some SQL...', @RowCount OUTPUT

However, you need to be very careful as you're opening yourself up to SQL injection attacks (what if @SQL contains something dodgy?).

Sign up to request clarification or add additional context in comments.

3 Comments

error when do select dbo.ExampleSproc ('select 1') - info >> Only functions and extended stored procedures can be executed from within a function. " -- i think here's the answer -- jimbojw.com/wiki/… seems impossible to exec dynamic sql in udf?
@elaine Yes, that's what I said above ;)
even using this way, I cannot meet the requirement. :) but thanks, I found an alternative.
1

Out of interest, why don't you just

select @@rowcount

after executing the query?

2 Comments

because it will always be 1 for a query like select count(*) from YourTable because there is only one row returned with the count of the entire table.
uuh ... ok, let me rephrase. Since the OP seems to want to exec an arbitrary SQL statement on a connection with the primary purpose in order to determine the rowcount from said query, why not consider @@rowcounting after executing the arbitrary statement on the same connection as an alternative.
0

To call dynamic SQL you have to use parantheses around the parameter:

EXEC (@SQL)

But you can't use a statement with side effects inside a UDF, so its impossible to execute dynamic SQL.

Besides this, its not possible to return a value from your dynamic SQL query and store it in a variable.

1 Comment

It is possible to execute dynamic sql and get the value into a variable, you just have to use sp_executesql per my answer. Obv not within a UDF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.