I have to Execute a dynamic SQL SELECT
Query And put Results into a #TempTable
.
DECLARE @StateId CHAR(3)='StateID';
DECLARE @DeptId CHAR(15)='DeptID';
DECLARE @Query VARCHAR(MAX)='Select Columns With Joins Passed From Front End'
DECLARE @Where VARCHAR(500);
DECLARE @FinalQuery VARCHAR(MAX)='';
SET @Where='Some Where Condition';
SET @FinalQuery='SELECT '+@Query+' '+@Where+''
EXEC(@FinalQuery) -- Want To INSERT THIS Result IN SOME `#TempTable`
-- SO that I can perform Some Operations On `#TempTable`
ALSO No Of columns returned From Dynamic SQL SELECT
Are Dynamic.
Thanks,
Please make sure that your query statements is correctly. Try this in your stored procedure and check the output results ( copy the result and execute it)
You can use the SQL Profiler tool to debug
try the below example
Note :If you need to use temtable after sp execution then use ## rather than # then we can access it or we can use persistent temporary table
Try this... First create the temp table which you need for further calculation.
CREATE TABLE #TEMP ( STATEID INT, DEPTID INT )