set multiple value with comma in single variable select data from table - select

set multiple value with comma in single variable select data from table
DECLARE #Values varchar(1000)
SET #Values = 'A, B, C'
SELECT
blah
FROM
foo
blah value like A, B, C, D, E..

DECLARE #Crossing1_Table TABLE (Crossing_No NVARCHAR(200), userid INT, GameName varchar(50),
GameDate date, CrossingQty INT, ccount int)
DECLARE #Id int
DECLARE #cross1 varchar(MAX)
DECLARE #ganame varchar(50)
DECLARE #gdate date
DECLARE #Cqty int
DECLARE cur_emp CURSOR
STATIC FOR
SELECT CrossingValue1, UserId, GameName, TodayDate, CrossingQty
FROM Game where GameListId=#Gamename and Cast(TodayDate as DATE)=#Gamedate
OPEN cur_emp
IF ##CURSOR_ROWS > 0
BEGIN
FETCH NEXT FROM cur_emp INTO #cross1, #Id, #ganame, #gdate, #Cqty
WHILE ##Fetch_status = 0
BEGIN
INSERT INTO #Crossing1_Table
SELECT CAST(Item AS INT), #Id, #ganame, #gdate, #Cqty, 1
FROM dbo.SplitString(#cross1, ',')
FETCH NEXT FROM cur_emp INTO #cross1, #Id, #ganame, #gdate, #Cqty
END
END
CLOSE cur_emp
DEALLOCATE cur_emp

Related

Out parameter is correctly assigned, but caller only sees a NULL. Clue?

I have this procedure that gets dropped/created as part of a T-SQL script - the idea is to insert a parent record, and output its ID to the caller so that I can insert children records using that ID.
if exists (select * from sys.procedures where name = 'InsertCategory')
drop procedure dbo.InsertCategory;
go
create procedure dbo.InsertCategory
#code nvarchar(5)
,#englishName nvarchar(50)
,#frenchName nvarchar(50)
,#timestamp datetime = null
,#id int output
as begin
if #timestamp is null set #timestamp = getdate();
declare #entity table (_Id int, EntityId uniqueidentifier);
declare #entityId uniqueidentifier;
if not exists (select * from dwd.Categories where Code = #code)
insert into dwd.Categories (_DateInserted, Code, EntityId)
output inserted._Id, inserted.EntityId into #entity
values (#timestamp, #code, newid());
else
insert into #entity
select _Id, EntityId from dwd.Categories where Code = #code;
set #id = (select _Id from #entity);
set #entityId = (select EntityId from #entity);
declare #english int;
set #english = (select _Id from dbo.Languages where IsoCode = 'en');
declare #french int;
set #french = (select _Id from dbo.Languages where IsoCode = 'fr');
exec dbo.InsertTranslation #entityId, #english, #englishName, #timestamp;
exec dbo.InsertTranslation #entityId, #french, #frenchName, #timestamp;
end
go
Then a little further down the script it's called like this:
declare #ts datetime;
set #ts = getdate();
declare #categoryId int;
exec dbo.InsertCategory 'C1', 'Category1', 'Catégorie1', #ts, #categoryId;
exec dbo.InsertSubCategory 'SC1', #categoryId, 'Description (EN)', 'Description (FR)', #ts
When I debug the script and step through line by line, I can see that dbo.InsertCategory correctly assigns the #id out parameter, which the script sees as #categoryId - the problem is that #categoryId is always null, and so I'm not getting anything inserted into dwd.SubCategories.
What am I doing wrong?
You need to mention the #categoryId parameter as OUTPUT while calling the procedure else it is not going to return the value. Call the procedure like this
exec dbo.InsertCategory 'C1', 'Category1', 'Catégorie1', #ts, #categoryId OUTPUT;
Example
CREATE PROCEDURE Procd (#a INT, #b INT output)
AS
BEGIN
SELECT #b = #a
END
DECLARE #new INT
EXEC Procd 1,#new
SELECT #new -- NULL
EXEC Procd 1,#new OUTPUT
SELECT #new -- 1

Stored procedure returns nullable int

In my procedure i am returning a int value.
ALTER PROCEDURE [dbo].[GetValue]
-- Add the parameters for the stored procedure here
#ID int,
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE #isNew int
SET #isNew=0
DECLARE #returnedValue int
DECLARE #output int
SET #returnedValue=[dbo].fn_GetIsNewLecturer(#ID)
IF(#returnedValue=0)
BEGIN
PRINT 'new'
EXEC #output=[dbo].[GetNew] #ID
SELECT #output
END
ELSE
BEGIN
PRINT 'old'
EXEC #output=[dbo].[sp_GetOld] #ID
SELECT #output
END
RETURN #output
END
it return value should be int. But it returns Nullable int?. how to change it as int
Try this:
select [Output] = isnull(#output, 0)
Here's why it should work:
declare #i int
select ni = #i, nni = isnull(#i,0)
into #t
select is_nullable, *
from tempdb.sys.columns
where [object_id] = object_id(N'tempdb..#t')
drop table #t

TSQL - How to iterate a list of strings

I want to create a procedure that will insert all my jobs to the DB.
(a. All my jobs have equal characteristics. b. SSDT doesn't support jobs code management)
Now, I thought to create a script to insert all of them and as a c# developer I thought I need to initialize a list with their names.
I discovered while googling that the way to do it is with an in memory table and the best I could come with is this.
declare #jobsNames table(Id int, JobName nvarchar(100))
insert into #jobsNames (Id,JobName)
select 1,'JobName1' union
select 2,'JobName2' union
......
BEGIN TRANSACTION
DECLARE JobsCursor CURSOR FOR SELECT JobName FROM #jobsNames
OPEN JobsCursor
FETCH NEXT FROM JobsCursor INTO #JobName
WHILE ##Fetch_status = 0
BEGIN
.. do stuff
FETCH NEXT FROM JobsCursor INTO #JobName
WHILE ##Fetch_status = 0
END
COMMIT TRANSACTION
Question -
Is this the shortest/recommended way?
(It seems a hellotof code for a foreach)
declare #jobNames table(Id int, JobName nvarchar(100))
insert #jobNames values
(1, 'JobName1'),
(2, 'JobName2'),
--
(10, 'JobName10')
while exists(select 1 from #jobNames)
begin
declare #id int, #name nvarchar(100)
select top 1 #id = Id, #name = JobName from #jobNames
delete from #jobNames where Id = #Id
-- Do stuff here
end
Personally I avoid Cursors like the plague. Please make sure that you HAVE to iterate instead of doing your work set based. They don't call it RBAR for nothing.
DECLARE #counter INT, #max INT
SELECT #counter = 1, #max = max(id)
FROM #jobsNames
WHILE #counter <= #max
BEGIN
SELECT #val1 = val1 ... FROM #jobNames where ID = #counter
-- .. do stuff
SET #counter = #counter + 1
END

How to use WITH table AS result within cursor loop to run stored procedure

How to get result in WITH table AS into CURSOR loop? I have previously asked about how to get recursive results from my table
How to read all records recursively and show by level depth TSQL
;with C as
(
definition ...
)
I have created CURSOR loop where I want to run specific stored procedure for all results in table
declare #id int, #parent int
declare cur cursor local fast_forward
for
select id, parent from C
open cur
fetch next from cur into #id, #parent
while ##fetch_status = 0
begin
exec storedProcedure #id=#id, #parent=#parent
fetch next from cur into #id, #parent
end
close cur
deallocate cur
Problem is that CURSOR doesnt know table from WITH AS result.
Invalid object name 'C'.
You can create a temp table or a table variable to hold the rows returned by you CTE query and then you use that table as the source for your cursor.
declare #T table
(
id int,
parent int
)
;with C as
(
select 1 as id, 2 as parent
)
insert into #T
select id, parent
from C
declare cur cursor for select id, parent from #T

Assign a list of integers to an #var

I can:
declare #idOrder int
set #idOrder = 21319
I want:
declare #idOrder int
set #idOrder = (21319, 21320)
for use in a series of statements where the 'WHERE' clause uses the IN operator
delete Orders
where idOrder in #idOrder
instead of
delete Orders
where idOrder in (21319, 21320)
You can't do that as long as it's an int, as that's not a valid value for that datatype. A datatype that could take several integers is a table
declare #idOrder table (id int)
insert into #idOrder values(21319)
insert into #idOrder values(21320)
delete from Orders where idOrder in (select id from #idOrder)
In SQL Server you can also
CREATE FUNCTION [dbo].[fn_ado_param_int] (#ado nvarchar(4000))
RETURNS #VALUES TABLE (ado int)AS
BEGIN
declare #Delim char(1)
set #Delim = ','
DECLARE #chrind INT
DECLARE #Piece nvarchar(4000)
SELECT #chrind = 1
WHILE #chrind > 0
BEGIN
SELECT #chrind = CHARINDEX(#Delim,#ado)
IF #chrind > 0
SELECT #Piece = LEFT(#ado,#chrind - 1)
ELSE
SELECT #Piece = #ado
INSERT #VALUES(ado) VALUES(#Piece)
SELECT #ado = RIGHT(#ado,LEN(#ado) - #chrind)
IF LEN(#ado) = 0 BREAK
END
RETURN
END
declare #idOrder varchar(500);
set #inOrder = "21319,2138,2138";
delete from Orders where id in (select ado from dbo.fn_ado_param_int(#idOrder));