How to store a query result in a variable

user8624282

SQL Server stored procedure:

CREATE PROCEDURE [dbo].[CheckTableStatus] 
    @DatabaseName AS NVARCHAR(50) = 'DBA',
    @ProjectID AS NVARCHAR(50) = 'CommandLog'
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @TableCount as int

    SET @Temp = 'DECLARE @cnt as int;'
    SET @Temp = @Temp + 'USE '+ @DatabaseName +'; SELECT @cnt=COUNT(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME=''' + @ProjectID + ''';'

    PRINT @Temp

    EXEC sp_executesql @temp

    --ASSIGN OUTPUT TO @TableCount
    IF @TableCount > 0  
       -- Do something
END

How do I assign the results of @temp being executed to variable @TableCount?

Andrei Odegov

Use the OUTPUT parameter:

CREATE PROCEDURE [dbo].[CheckTableStatus] 
    @DatabaseName as nvarchar(50) = 'DBA',
    @ProjectID as nvarchar(50) = 'CommandLog'
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @TableCount as int,@Temp nvarchar(max)='';
--    SET @Temp = 'DECLARE @cnt as int;'
    SET @Temp = @Temp + 'USE '+ @DatabaseName +'; SELECT @cnt=COUNT(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME=''' + 
@ProjectID + ''';'
    print @Temp
    EXEC sp_executesql @temp,
      N'@cnt int out',@TableCount out;
    --ASSIGN OUTPUT TO @TableCount
    IF @TableCount >0 
     -- Do something
        print @TableCount;
END

But why not use the OBJECT_ID function?

Also your code is prone to SQL-injection.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to store query result (a single document) into a variable?

From Dev

how to store the query result into a variable in mysql

From Dev

Store the result of a Dynamic Query in a variable

From Dev

How do I store a mongodb query result in a variable?

From Dev

JSP,mysql - How to get and store variable from a fresh query result

From Dev

How to store SQL query result in a variable using PHP?

From Dev

How to store the result of a Firebase query to a variable from a promise?

From Dev

How to store query result in a variable in MySql stored Procedure

From Dev

How to store SensorThings query result into a variable in JavaScript/jQuery

From Dev

How to store a query result into a variable in mysql and then use it another query and echo result?

From Dev

store result of select query into array variable

From Dev

store the result of xpath into an variable to assist in future query

From Dev

Store a sql query result in pentaho variable

From Dev

How to declare variable with result of query?

From Dev

How to assign the result of a query into a variable?

From Dev

How to put result of query in a variable?

From Dev

How to put result of query in a variable?

From Dev

How to store MySQL query result in a string

From Dev

How to store SQL Query result in table column

From Dev

How to store and process result of a query in PHP array?

From Dev

Ansible: how to store the result of a json_query?

From Dev

How to store in a variable an echo | cut result?

From Dev

how to store result of tail command in variable?

From Dev

How to store sed result in variable in tcsh

From Dev

Python - How to store a Search Result (list) in a variable?

From Dev

how to store the result of a set operation into a variable

From Dev

How to store sed result in variable in tcsh

From Dev

How to store the result of dynamic SQL in a variable?

From Dev

How can I store the result of a function into a variable?

Related Related

  1. 1

    How to store query result (a single document) into a variable?

  2. 2

    how to store the query result into a variable in mysql

  3. 3

    Store the result of a Dynamic Query in a variable

  4. 4

    How do I store a mongodb query result in a variable?

  5. 5

    JSP,mysql - How to get and store variable from a fresh query result

  6. 6

    How to store SQL query result in a variable using PHP?

  7. 7

    How to store the result of a Firebase query to a variable from a promise?

  8. 8

    How to store query result in a variable in MySql stored Procedure

  9. 9

    How to store SensorThings query result into a variable in JavaScript/jQuery

  10. 10

    How to store a query result into a variable in mysql and then use it another query and echo result?

  11. 11

    store result of select query into array variable

  12. 12

    store the result of xpath into an variable to assist in future query

  13. 13

    Store a sql query result in pentaho variable

  14. 14

    How to declare variable with result of query?

  15. 15

    How to assign the result of a query into a variable?

  16. 16

    How to put result of query in a variable?

  17. 17

    How to put result of query in a variable?

  18. 18

    How to store MySQL query result in a string

  19. 19

    How to store SQL Query result in table column

  20. 20

    How to store and process result of a query in PHP array?

  21. 21

    Ansible: how to store the result of a json_query?

  22. 22

    How to store in a variable an echo | cut result?

  23. 23

    how to store result of tail command in variable?

  24. 24

    How to store sed result in variable in tcsh

  25. 25

    Python - How to store a Search Result (list) in a variable?

  26. 26

    how to store the result of a set operation into a variable

  27. 27

    How to store sed result in variable in tcsh

  28. 28

    How to store the result of dynamic SQL in a variable?

  29. 29

    How can I store the result of a function into a variable?

HotTag

Archive