Dynamic Stored Procedure Results to Table - sql-server

I have a stored procedure that dynamically produces pivot results, passing sql for row defs, column to pivot, aggregate(field) to sum, and table name of aggregate. This works great, but i need to produce a table from these results to use in further calculations.
How can I dynamically save the results to a table within the stored procedure (temp or non temp) without knowing the output columns??

SELECT *
INTO #TempTable
FROM (Pivot Expression)
This will create a #TempTable with the results of whatever you have in the FROM clause, regardless of number/type of columns.

you didn't ask, but this is how I'm getting a set of column names from a view:
DECLARE #columns VARCHAR(1000)
SELECT #columns = COALESCE(#columns + ',[' + cast(fld as varchar) + ']',
'[' + cast(fld as varchar)+ ']')
FROM view
GROUP BY fld

Related

Dynamically PIVOT on ONE AND ONLY ONE column from a single TABLE

I've read multiple threads on this topic, and there does seem to be a way to do this dynamically, but I'm getting a syntax/compile error on the code.
I am trying to dynamically pull multiple rows for a temp table that simply has one column. The definition of the temp table is one field called acct_name. The challenge seems to be the "dynamics" of the fact that there will be over 50+ rows in the temp table, which is subject to change at any time.
I followed a previous poster's example, but am still getting compile/runtime errors.
I'm a rather novice SQL person, so you'll have to excuse the crudity of my code or my question.
DECLARE #idList varchar(500)
SELECT #idList = COALESCE(#idList + ',', '') + acct_name
FROM ##tmp_accts
DECLARE #sqlToRun varchar(1000)
SET #sqlToRun = '
SELECT *
FROM (
SELECT acct_name FROM ##tmp_accts
) AS src
PIVOT (
MAX(acct_name) FOR acct_name IN ('+ #idList +')
) AS pvt'
EXEC (#sqlToRun)
Does anyone have an obvious suggestion, I think it's very close to working.....
FOR EXAMPLE,
Let's say for sake of example we have the following acct_names - '12345','23456','34567','45678'.
The desire result is to return one row with 4 columns each with the respective value of acct_name. HOWEVER, the acct name is dynamic and is not known in advance, nor is the count of acct_name known in advance. A temp table is generated on the fly which determines all of the relevant acct_names for that particular run. It will vary with each run, each day that the query is run.
Thank you.....
Thru an article available thru Microsoft, the following solution does the job apporpriately.....
DECLARE
#columns NVARCHAR(MAX) = '',
#sql NVARCHAR(MAX) = '';
-- select the category names
SELECT
#columns+=QUOTENAME(acct_name) + ','
FROM
##tmp_accts
GROUP BY
acct_name;
-- remove the last comma
SET #columns = LEFT(#columns, LEN(#columns) - 1);
-- construct dynamic SQL
SET #sql ='
SELECT * FROM
(
SELECT DISTINCT acct_name
FROM
##tmp_accts
) t
PIVOT(
COUNT(acct_name)
FOR acct_name IN ('+ #columns + ')
) AS piv;';
-- execute the dynamic SQL
EXECUTE sp_executesql #sql;

SSRS Temporary table issue, data from stored procedure which include dynamic pivot. (SQL 2008/Visual Studio 2008)

I have a stored procedur which runs ok from SQL level but when I am tryning to
add dataset in SSRS there is an issue with temp table. I know that SSRS has a problem with temp tables (in my case global table) but how can I resolve this problem? I saw few solutions suggests to create #table but how can I do that with data from dynamic pivot and I am not sur of it reaaly works?
Here is example of my dynamic pivot:
DECLARE #PmtCols_pwyk AS NVARCHAR(MAX), #query_pwyk AS NVARCHAR(MAX)
select #PmtCols_pwyk = STUFF((SELECT ',' + QUOTENAME(przerwa)
from #przerwa_wyk
group by przerwa
order by przerwa
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
if OBJECT_ID ('tempdb..##pwyk_po_pivocie') is not null drop table ##pwyk_po_pivocie
set #query_pwyk = 'SELECT Id_Pracownika,' + #PmtCols_pwyk + ' into ##pwyk_po_pivocie from
(select przerwa, Id_Pracownika, przerwa_wyk from #przerwa_wyk ) as x
PIVOT
(max(przerwa_wyk) for przerwa in (' + #PmtCols_pwyk + ')) as p'
execute(#query_pwyk) ;

How to SUM dynamic field names from temp table

I have table a with bunch of 0's scattered across many columns which are dynamically named dates (e.g. 2017_09_28). I want to hide the rows where the sum of all of these dynamically named columns nets to 0.
Assuming I have to create a select statement...
SELECT * FROM (
Select *, (dynamic column 1 + dynamic column 2 + dynamic column 3) sum
FROM temp table) sbqy WHERE sum != 0
Not sure how to grab all of the dynamic column names! If there is an alternative solution, all ears, but cannot spell out each dynamic column, too many and will change week to week.
You can use dynamic sql to create a string and then run it with sp_executesql
declare #cmd varchar(4000) = ''
set #cmd = 'select * from temptable where ' + case when someLogicHere then 'dynamicColomn1' end + case when someLogicHere then '+ dynamicColomn2' end
+ case when someLogicHere then 'dynamicColomn3' end + ' !=0'
--print(#cmd)
exec sp_executesql #cmd

Creating a Table Valued Function with dynamic columns

I received help previously on creating a query for listing total sales by month for a list of jobs. However, I cannot put this into a view as a variable needs declared.
I thought I could use a Table Valued Function (which I've never used before) but apparently I need to define the columns of the table which I cannot do if they are to be dynamic (the columns are yyyy-mm).
Can someone suggest how I could approach this problem please?
I am not familiar with creating anything other than views so this is completely new to me. Obviously if using the TVF isn't the best method please advise.
The query:
Declare #SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(YearMonth) From v_JobSalesByMonth Order by 1 For XML Path('')),1,1,'')
Select #SQL = '
Select [JobID], [TotalJobSales],' + #SQL + '
From (
Select JobID
,TotalJobSales = sum(SalesForMonth) over (Partition By JobID)
,YearMonth
,SalesForMonth
From v_JobSalesByMonth A) A
Pivot (sum(SalesForMonth) For [YearMonth] in (' + #SQL + ') ) p'
Exec(#SQL);
I started to create the function but am confused about how to define the dynamic columns.
CREATE FUNCTION db.tvfnJobSalesByMonthPivot (#JobID INT)
RETURNS #JobSalesByMonth TABLE
(
JobID int PRIMARY KEY NOT NULL,
TotalJobSales decimal(6,2) NULL,
2016-12 (ermmmmm?)
)
Many thanks in advance.
Paul

SQL to split Comma Separated values and compare it with a multi list value in SSRS

I have a field in my table which has multiple reason codes concatenated in 1 column.
e.g. 2 records
Reason_Codes
Record1: 001,002,004,009,010
Record2: 001,003,005,006
In my SSRS report the user will be searching for data using one of the above reason codes. e.g.
001 will retrieve both records.
005 will retrieve the second record
and so on.
Kindly advise how this can be achieved using SQL or Stored Procedure.
Many thanks.
If you are just passing in a single Reason Code to search on, you don't even need to bother with splitting the comma-separated list: you can just use a LIKE clause as follows:
SELECT tb.field1, tb.field2
FROM SchemaName.TableName tb
WHERE ',' + tb.Reason_Codes + ',' LIKE '%,' + #ReasonCode + ',%';
Try the following to see:
DECLARE #Bob TABLE (ID INT IDENTITY(1, 1) NOT NULL, ReasonCodes VARCHAR(50));
INSERT INTO #Bob (ReasonCodes) VALUES ('001,002,004,009,010');
INSERT INTO #Bob (ReasonCodes) VALUES ('001,003,005,006');
DECLARE #ReasonCode VARCHAR(5);
SET #ReasonCode = '001';
SELECT tb.ID, tb.ReasonCodes
FROM #Bob tb
WHERE ',' + tb.ReasonCodes + ',' LIKE '%,' + #ReasonCode + ',%';
-- returns both rows
SET #ReasonCode = '005';
SELECT tb.ID, tb.ReasonCodes
FROM #Bob tb
WHERE ',' + tb.ReasonCodes + ',' LIKE '%,' + #ReasonCode + ',%';
-- returns only row #2
I have blogged about something like this a long time ago. May be this will help: http://dotnetinternal.blogspot.com/2013/10/comma-separated-to-temp-table.html
The core solution would be to convert the comma separated values into a temporary table and then do a simple query on the temporary table to get your desired result.

Resources