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

Kanarinox

I have the following theoretical statement which I would like to implement using dynamic SQL (on SQL Server 2016) and store the single output value in the variable (@output).

@numericvar, @columnname, @tablename should be input parameters. Any help would be greatly appreciated. Many thanks.

SELECT @output = (
    SELECT 
        MAX(CASE WHEN ROWNUM*1.0/NUMROWS <= @numericvar THEN @columnname END)
    FROM (
        SELECT
            @columnname,
            ROW_NUMBER() OVER (ORDER BY @columnname ) AS ROWNUM,
            COUNT(*) OVER (PARTITION BY NULL) AS NUMROWS
        FROM 
            @tablename 
    ) @tablename 
); 
TT.
DECLARE @columnname SYSNAME, @tablename SYSNAME, @numericvar NUMERIC(18,2);
DECLARE @output NUMERIC(18,2);

DECLARE @sql NVARCHAR(MAX) = N'
    SET @output = (
        SELECT 
            MAX(CASE WHEN ROWNUM*1.0/NUMROWS <= @numericvar THEN '+QUOTENAME(@columnname)+N' END)
        FROM (
            SELECT
                '+QUOTENAME(@columnname)+N',
                ROW_NUMBER() OVER (ORDER BY '+QUOTENAME(@columnname)+N' ) AS ROWNUM,
                COUNT(*) OVER (PARTITION BY NULL) AS NUMROWS
            FROM 
                '+QUOTENAME(@tablename)+N'
        ) AS t
    );
';

EXECUTE sp_executesql 
    @sql,
    N'@numericvar NUMERIC(18,2), @output NUMERIC(18,2) OUTPUT', 
    @numericvar, @output OUTPUT;

SELECT @output;

Update: a working example for FLOAT output. The script uses a table in the INFORMATION_SCHEMA schema which everyone has.

See if you can make it work from this sample. If you can't I suggest you edit your question, and add the exact script + parameter values you are using + indication of the type of columnname.

DECLARE @schemaname SYSNAME='INFORMATION_SCHEMA',
        @tablename  SYSNAME='COLUMNS', 
        @columnname SYSNAME='NUMERIC_PRECISION', 
        @numericvar NUMERIC(18,2)=.5;

DECLARE @output_f FLOAT;

DECLARE @sql NVARCHAR(MAX) = N'
    SET @output_f = (
            SELECT 
                MAX(CASE WHEN ROWNUM*1.0/NUMROWS<=@numericvar THEN '+QUOTENAME(@columnname)+N' END)
            FROM (
                SELECT
                    '+QUOTENAME(@columnname)+N',
                    ROW_NUMBER() OVER (ORDER BY '+QUOTENAME(@columnname)+N') AS ROWNUM,
                    COUNT(*) OVER () AS NUMROWS
                FROM 
                    '+QUOTENAME(@schemaname)+N'.'+QUOTENAME(@tablename)+N'
            ) AS t
    );
';

EXECUTE sp_executesql 
    @sql,
    N'@numericvar NUMERIC(18,2), @output_f FLOAT OUTPUT', 
    @numericvar, @output_f OUTPUT;

SELECT @output_f;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Store the result of a Dynamic Query in a variable

From Dev

How to store values from a dynamic sql into a variable?

From Dev

How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

From Dev

Cannot store result into variable SQL

From Dev

How to store select statement result to table variable in sql server

From Dev

How to store result of stored procedure in a variable using SQL Server

From Dev

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

From Dev

How to store a query result in a variable

From Dev

Store a sql query result in pentaho variable

From Dev

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

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 query result into a variable in mysql

From Dev

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

From Dev

Javascript - How to store result of the for loop into the variable?

From Dev

How to store SQL Query result in table column

From Dev

How to get a result from dynamic SQL in Postgres?

From Dev

Store column result in variable

From Dev

Store function result into variable

From Dev

Store the result of a command in a variable

From Dev

store into variable a sudo result

From Dev

Store function result into variable

From Dev

Store the first result value into a variable from sql query in php

From Dev

Trying to store result in xml format in variable sql server

From Dev

Store the first result value into a variable from sql query in php

Related Related

  1. 1

    Store the result of a Dynamic Query in a variable

  2. 2

    How to store values from a dynamic sql into a variable?

  3. 3

    How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

  4. 4

    Cannot store result into variable SQL

  5. 5

    How to store select statement result to table variable in sql server

  6. 6

    How to store result of stored procedure in a variable using SQL Server

  7. 7

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

  8. 8

    How to store a query result in a variable

  9. 9

    Store a sql query result in pentaho variable

  10. 10

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

  11. 11

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

  12. 12

    how to store result of tail command in variable?

  13. 13

    How to store sed result in variable in tcsh

  14. 14

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

  15. 15

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

  16. 16

    How to store sed result in variable in tcsh

  17. 17

    how to store the query result into a variable in mysql

  18. 18

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

  19. 19

    Javascript - How to store result of the for loop into the variable?

  20. 20

    How to store SQL Query result in table column

  21. 21

    How to get a result from dynamic SQL in Postgres?

  22. 22

    Store column result in variable

  23. 23

    Store function result into variable

  24. 24

    Store the result of a command in a variable

  25. 25

    store into variable a sudo result

  26. 26

    Store function result into variable

  27. 27

    Store the first result value into a variable from sql query in php

  28. 28

    Trying to store result in xml format in variable sql server

  29. 29

    Store the first result value into a variable from sql query in php

HotTag

Archive