1

I have a table with a column containing queries to get count. I am writing a stored procedure to fetch the records from this table. What i need to do is to execute the queries in the count column and return the value in the same column. Below is a sample table with the count column.

So, when i run the stored procedure, i should get the result like this :

enter image description here

So, can you please suggest me if there is any way to implement this in sql server 2008.

5
  • 1
    I don't understand. You have queries inside a column of a table to get count? That doesn't make sense to me. Commented Mar 2, 2015 at 6:35
  • This is a sample table.. in my real project the count column is similar with different queries based on the table. I need to know if it is possible to execute these queries and get the result as i need. Commented Mar 2, 2015 at 6:37
  • 1
    Surely there must be a better design for this. Have you looked at stored procedures or user defined functions? Storing queries as table values is not exactly good practice. Commented Mar 2, 2015 at 6:39
  • Well you can do that with loading the data into a temp. table, then use cursor + sp_execute_sql, but that design is quite horrible... Commented Mar 2, 2015 at 6:43
  • Look at Computed Columns and see if that helps. Otherwise, Views might help you as well. Commented Mar 2, 2015 at 7:00

2 Answers 2

1

Despite the fact that this design is horrible, here is the way you can do what you need:

CREATE TABLE Users(ID int)
GO

CREATE TABLE Test(ID INT, Name NVARCHAR(20), Count NVARCHAR(200))
GO

INSERT  INTO Users
VALUES  ( 1 ),
        ( 1 ),
        ( 2 ),
        ( 3 )

INSERT  INTO Test
VALUES  ( 1, 'A', 'Select Count(*) From Users where ID = 1' ),
        ( 2, 'B', NULL ),
        ( 3, 'C', 'Select Count(*) From Users where ID = 3' ),
        ( 4, 'D', 'Select Count(*) From Users where ID = 1' )


DECLARE @Results TABLE
    (
      ID INT ,
      Name NVARCHAR(20) ,
      Count INT
    )
DECLARE @R TABLE ( ID INT )
DECLARE @ID INT ,
    @Name NVARCHAR(20) ,
    @Statement NVARCHAR(200) ,
    @Count INT

DECLARE cur CURSOR FAST_FORWARD READ_ONLY
FOR
    SELECT  ID , Name , Count FROM    Test

OPEN cur

FETCH NEXT FROM cur INTO @ID, @Name, @Statement

WHILE @@FETCH_STATUS = 0
    BEGIN

        DELETE  FROM @R

        INSERT  INTO @R
                EXEC ( @Statement )

        INSERT  INTO @Results
        VALUES  ( @ID, @Name, ( SELECT  * FROM    @R ) )  

        FETCH NEXT FROM cur INTO @ID, @Name, @Statement

    END

CLOSE cur
DEALLOCATE cur

SELECT  * FROM    @Results

Output:

ID  Name  Count
1   A     2
2   B     NULL
3   C     1
4   D     2
Sign up to request clarification or add additional context in comments.

6 Comments

Why to force doors wide open? Have you seen my answer?
@MaciejLos, your answer doesn't answer the OP's question i need to do is to execute the queries in the count column....if there is any way to implement this.... Besides that you are creating stored procedure to select counts, but you can select that information in OUTER APPLY for example.. No need for stored proc
Doesn't answer? Op wants to get the count of users... On the other side: i do not see the reason to call several queries, even on performance aspect.
@MaciejLos, OP doesn't want counts of users(This is a sample table.. in my real project the count column is similar with different queries based on the table). He wants TO EXECUTE QUERY. Also you are talking about performance, but implemented solution with stored procedures. Can you write the query that selects from tables and get desired data with your solution? In your solution at least scalar valued function would be better(not to mention OUTER APPLY)
@GiorgiNakeuri I do realize now that the design is horrible :) Just wanted to give this a try. Thanks for providing the solution to make the judgement call.
|
1

I agree with Sorrel Vesper. I do not understand why do you want to store query in a table.

Instead of this use/create proper SP:

CREATE PROCEDURE GetUserCount
    @userId INT
AS
BEGIN

    SET NOCOUNT ON;
    SELECT COUNT(*) AS UserCount
    FROM TableName
    WHERE UserId = @userId

END

For further information, please see: CREATE PROCEDURE

In case you want to fetch data for every single user, use query like this:

SELECT UserId, COUNT(*)
FROM TableName
GROUP BY UserId

That's all!

For further information, please see: Aggregate functions

Comments

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.