I create the same SQL script, which must count number of records in my table. I'm using dynamic sql. For example:
... declare @myTableName string; declare @query sqtring; @myTableName = 'table_1'; @query = 'select count(*) from ' + @myTableName; executive immediate(@query); ...
This query running good, but I don't know how can I get result from:
executive immediate(@query);
I tried to find answer on the Internet, but I does not find answer.
May be anybody know answer on my question?
Thank.
Using a temporary table, this can be done like this:
DECLARE @IntVal Integer;
DECLARE @myTableName string;
DECLARE @query string;
CREATE TABLE #table1 (
Value Integer
);
@myTableName = 'table_1';
@query = 'INSERT INTO #table1 (Value) SELECT COUNT(*) FROM ' + @myTableName;
EXECUTE IMMEDIATE @query;
@IntVal = (SELECT Value FROM #table1);