So I am performing a while loop on 3 stored procs and inserting the data into temp tables and then performing a union on them and when I run the query It returns 10 results but when I make a report out of it in SSRS it returns just the first result and stops! shouldn't it make multiple pages with the other results?
declare #temptable table ( id int )
while (select count(*) from customerid c left join #temptable t on c.id = t.id where t.id is null) > 0 begin
insert into #temptable select min(c.id) from customerid c left join #temptable t on c.id = t.id where t.id is null
declare #id2 varchar(50) = (select max(id) from #temptable)
-- insert into temp tables here
exec DoctorsSDJs #id = #id2
-- insert into temp table
exec LocationSDJs #id = #id2
-- insert into temp tables
exec PatientSDJs #id = #id2
-- performing my union on the 3 tables
end
Related
This query was from a 2017 post on this site and the results fit my requirements but I need them to a temp table. It currently writes to the screen with the column headers from each table and the values in each. When I try and insert it onto a temp table I can't get the column names, only the values. Basically I'm comparing two tables and need to report the differences in them. Your assistance is greatly appreciated.
DECLARE #ColName varchar(100)
DECLARE #Table1 varchar(100) = 'MyTable'
DECLARE #Table2 varchar(100) = 'MyOtherTable'
IF (OBJECT_ID('tempdb..#col') IS NOT NULL) DROP TABLE #col
SELECT IDENTITY(INT, 1, 1) RowNum , c.name
INTO #col
FROM SYS.Objects o
JOIN SYS.columns c on o.object_id = c.object_id
WHERE o.name = #Table1 AND NOT c.Name IN ('List','Columns','YouWantToIgnore')
DECLARE #Counter INT = (SELECT MAX(RowNum) FROM #col)
WHILE #Counter > 0
BEGIN
SET #ColName = (SELECT name FROM #Col WHERE RowNum= #Counter)
EXEC ('SELECT t1.Identifier
,t1.'+#ColName+' AS '+#Table1+#ColName+'
,t2.'+#ColName+' AS '+#Table2+#ColName+'
FROM '+#Table1+' t1
LEFT JOIN '+#Table2+' t2 ON t1.Identifier = t2.Identifier
WHERE t1.'+#ColName+' <> t2.'+#ColName)
SET #Counter = #Counter - 1
END
I am trying to convert a cursor from SQL Server to BigQuery Standard SQL. Can we do it using a while loop? What is the best approach to write below cursor scenario in BigQuery? Thanks in advance!
declare #t1 int, #t2 int, #t3 int, #id int
declare #name varchar(30), #status varchar(30)
declare #cursor cursor
set #cursor = cursor for select * from table_A
open #cursor
fetch next from #cursor into #t1, #t2, #t3, #id, #name, #status
while ##fetch_status = 0
begin
delete from table_B where id = #id
update table_B b
set b.time = gettime() /*this can give different results if I try batch processing*/
where b.name = #name
and b.status = #status
if (#t1 is null and #t2 is null)
begin
insert into table_C (parent, child)
select 0, 0
end
if (#t2 is null and #t3 is null)
begin
insert into table_C (parent, child, level, is_above, is_below) /*this insert is different from above insert*/
select #t1, #t2, 3, 2, 1
end
fetch next from #cursor into #t1, #t2, #t3, #id, #name, #status
end
close #cursor
deallocate #cursor
As Sabri noted - you DON't need Cursor / loop here at all
So below script (BigQuery Standard SQL) resembles your initial logic in its simplistic way just to match your in-loop operations one-to-one
DELETE FROM table_B
WHERE id IN (SELECT DISTINCT id FROM table_A)
UPDATE table_B b
SET time = CURRENT_TIMESTAMP()
FROM table_A a
WHERE b.name = a.name
AND b.status = a.status
INSERT INTO table_C (parent, child)
SELECT 0, 0
FROM table_A
WHERE t1 IS NULL
AND t2 IS NULL
INSERT INTO table_C (parent, child, level, is_above, is_below)
SELECT t1, t2, 3, 2, 1
FROM table_A
WHERE t2 IS NULL
AND t3 IS NULL
I have this table with 22 rows as above
I am getting a converted table as above
I created following procedure in order to get result table
ALTER PROCEDURE [dbo].[proc_YS_BAB_IR_Item]
#UserId int
AS
BEGIN TRAN
DECLARE #SqlQry NVARCHAR(MAX);
SET #SqlQry = N''
DECLARE #Cnt INT = 1, #EndCnt INT = 25,
#v_UserId INT = CAST(#UserId AS VARCHAR(MAX));
CREATE TABLE #TempColumns
(
Calculate_ItemIdentifier VARCHAR(MAX),
SeqOrder INT
)
WHILE #cnt <= #EndCnt
BEGIN
INSERT INTO #TempColumns
SELECT
'IR'+ CAST(#Cnt AS VARCHAR(MAX)),
#Cnt
SET #Cnt = #Cnt + 1;
END
DECLARE #DATA VARCHAR(10), #DATA1 VARCHAR(10) = '000000'
DECLARE #zero_str VARCHAR(6) = '000000'
-- Generate table alike to yours
DECLARE #yourTable TABLE ([value] varchar(max))
-- convert array to xml
;WITH cte AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn,
[response],
CAST('<a>'+REPLACE(SUBSTRING([response],2,LEN([response]) - 2),',','</a><a>')+'</a>' as xml) AS x,
#v_UserId AS UserId,
[item_identifier]
FROM
#TempColumns
LEFT JOIN
YS_BAB_response_dump ON YS_BAB_response_dump.Item_Identifier = #TempColumns.Calculate_ItemIdentifier
AND UserId = CAST(#UserId AS VARCHAR(MAX))
AND TestModel = 'IR'
)
-- do the stuff
SELECT
c.rn,
c.[response],
c.[item_identifier],
RIGHT(#zero_str +
CAST(SUM(CAST(STUFF(#zero_str,t.c.value('.','tinyint')+1,1,'1') AS INT)) AS VARCHAR(6)), 6) AS ans,
c.UserId
FROM
cte c
CROSS APPLY
x.nodes('/a') AS t(c)
GROUP BY
c.rn, c.[response], c.UserId, c.[Item_Identifier]
ORDER BY
c.rn
COMMIT TRAN
What changes I need to do in above procedure to get 25 records as a result instead of 22, For IR21,IR22 and IR25 I want null data in response and ans columns for that respective userId? May i need to use some other function instead of CROSS APPLY? How it will be?
You were almost there. Use OUTER APPLY instead of CROSS APPLY
How can I using something like if or case..when to combine the following code into one?
if #para = 'test'
begin
select * from Table A where status='A' and id in (select id from Table B)
end
else if #para = 'others'
begin
select * from Table A where status='A' and id in (select id from Table c)
end
like select * from Table A where id in if #para = XXX then (select id from Table B)
Thanks a lot.
Try this:
select *
from Table A
where (id in (select id from Table B) and #para = 'test')
OR
(id in (select id from Table c) and #para = 'others')
As you tagged stored procedure i made it as one ... try it like this
create procedure GetData (#para nvarchar(100))
as
begin
declare #sql nvarchar(max)
set #sql = case
when #para = 'test'
then
'Select * from TableA A
join TableB B on A.id = B.ID'
else -- if #para = 'others' goes into else
'Select * from TableA A
join TableB B on A.id = B.ID'
end
execute (#sql)
end
I have this procedure:
USE myDataBase
DECLARE
#id INT = NULL
,#name VARCHAR(250)= NULL
,#id2 INT = NULL
,#flag INT =NULL
SELECT *
FROM dbo.table1 INNER JOIN
dbo.table2 ON id = id2
WHERE (#id IS NULL OR #id = id)
AND (#name IS NULL OR #name = name)
AND (#id2 IS NULL OR #id2 = id2)
Depending on what #flag contains, I need #id2 in where clause to be in a centain range.
Meaning
if #flag = 2 --#flag can contain any value from 1 to 12
I need this line:
AND (#id2 IS NULL OR #id2 = id2)
To be:
AND (#id2 IS NULL OR #id2 IN (61,62,63))
Also:
if #flag = 4 --#flag can contain any value from 1 to 12
I need this line:
AND (#id2 IS NULL OR #id2 = id2)
To be:
AND (#id2 IS NULL OR #id2 IN (74,75,76))
And so on..
How can I do that?
I know is a stupid question, but I cannot make it work :(
Thanks
How about:
AND ((#flag = 2 AND #id2 IS NULL OR #id2 IN (61,62,63)) OR
(#id2 IS NULL OR #id2 = id2) )
EDIT:
AND (((#flag >= 1 AND #flag <=12) AND #id2 IS NULL OR #id2 IN (61,62,63)) OR
(#id2 IS NULL OR #id2 = id2) )
Create a temporary table variable, and query using the contents of that variable.
DECLARE #IdList TABLE
(
Id INT,
)
-- Populate temporary table variable with required Id's depending on #flag value
IF #flag = 1
BEGIN
INSERT INTO #IdList VALUES (#id2)
END
IF #flag = 2
BEGIN
INSERT INTO #IdList VALUES (61), (62), (63)
END
IF #flag = 4
BEGIN
INSERT INTO #IdList VALUES (74), (75), (76)
END
-- Code for other flag values within 1-12
-- Perform query to get results
IF (SELECT COUNT(*) FROM #Temp) = 0
BEGIN
-- No values provided for matching
SELECT *
FROM dbo.table1
INNER JOIN dbo.table2 ON id = id2
WHERE (#id IS NULL OR #id = id)
AND (#name IS NULL OR #name = name)
END
ELSE
BEGIN
-- One or more values provided for matching
SELECT *
FROM dbo.table1
INNER JOIN dbo.table2 ON id = id2
AND id2 IN (SELECT Id FROM #IdList)
WHERE (#id IS NULL OR #id = id)
AND (#name IS NULL OR #name = name)
END
It's not clear how you're handling #id2 = NULL so the final IF clause is to separate the two.