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
Related
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
I have 2 Triggers. Separately, each Trigger works but not together.
Error: Msg 16915, Level 16, State 1, Procedure PrumerAsistence, Line 16
A cursor with the name 'curff' already exists.
The statement has been terminated.
TRIGGER 1:
create trigger PrumerAsistence on Hrac FOR UPDATE as
BEGIN
declare #old DATE
declare #new date
declare #YearOld int
declare #YearNew int
declare #Year int
declare #Asistence float
declare #id int
declare #as float
declare curff cursor for (
select inserted.hid
from inserted
)
open curff
fetch from curff into #id
while ##fetch_status = 0
BEGIN
set #old = (select min(datum)from prestup where #id = prestup.Hrac_hid)
set #new = (select max (datum) from prestup where #id = prestup.Hrac_hid)
set #YearOld = (select Year(#old))
set #YearNew = (select Year(#new))
set #Year = #YearNew- #YearOld
set #as = (
select sum (Asistence)/ #Year
from hrac
join prestup p on p.hrac_hid = hrac.hid
where hid = #id )
update Hrac set Prumer_Asistence = #as where hid = #id
fetch from curff into #id
END
close curff
deallocate curff
TRIGGER 2:
create trigger PrumerGolu on Hrac for UPDATE as
BEGIN
declare #old DATE
declare #new date
declare #YearOld int
declare #YearNew int
declare #Year int
declare #Gol float
declare #id int
declare #as float
declare cur cursor for (
select inserted.hid
from inserted
)
open cur
fetch from cur into #id
while ##fetch_status = 0
BEGIN
set #old = (select min(datum)from prestup where #id = prestup.Hrac_hid)
set #new = (select max (datum) from prestup where #id = prestup.Hrac_hid)
set #YearOld = (select Year(#old))
set #YearNew = (select Year(#new))
set #Year = #YearNew- #YearOld
set #gol = (
select sum (gol)/ #Year
from hrac
join prestup p on p.hrac_hid = hrac.hid
where hid = #id )
update Hrac set Prumer_Golu = #gol where hid = #id
fetch from cur into #id
END
close cur
deallocate cur
You should not use the same name for two different table cursor since they are happening at the same time.
MORE IMPORTANT:
It is not recommended to use the cursor in your case because of the tuning activity. ##fetch_status is a global variable, the scope to it across all the sessions and all batches. It may and it will cause the conflicts and make the result inaccurate.
You'd better to combine these two triggers together to let the SQL executer consider which cursor need to be took care of first, otherwise, may bring the data loss, etc.
Make sure you declare each cursor with the LOCAL option.
Full details on how to do this can be found here, as well as info on how to check what your default setting is (I'm guessing GLOBAL).
https://technet.microsoft.com/en-us/library/ms189238(v=sql.105).aspx
This is my project to school and i HAVE TO use 2 trigger. I know why but i have to.
Cant use Local. Msg 217, Level 16, State 1, Procedure PrumerGolu, Line 38
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).
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
I have a column of type *NVARCHAR which holds SQL statement snippets instead of the final values. So for example one of the fields holds the following snippet:
CAST(#originalValue * -1 AS INT)
What I am trying to accomplish is to use these SQL snippets to be inserted into stored procedure code latter on. To validate the functionality I have created the following very simple example to set an integer value to (5) and use the above SQL snippet within dynamic SQL statement to invert this value (5) into (-5).
DECLARE #originalValue AS INT = 5
DECLARE #fianalValue AS INT
DECLARE #sql AS NVARCHAR(50)
DECLARE #value AS NVARCHAR(50) = 'CAST(#originalValue * -1 AS INT)'
SET #sql = 'SELECT #fianalValue = '+CAST(#value AS NVARCHAR(100))+''
exec sp_executesql #sql, N'#fianalValue INT OUTPUT', #fianalValue = #fianalValue OUTPUT
PRINT '#fianalValue: ' + CAST(#fianalValue AS VARCHAR(50))
However, I am getting an error. I have tried in a few different approaches but something is not lining up. Thank for help.
Try this
DECLARE #originalValue AS INT = 5
DECLARE #fianalValue AS INT
DECLARE #sql AS NVARCHAR(50)
DECLARE #value AS NVARCHAR(50) = 'CAST('+convert(varchar(20),#originalValue)+' * -1 AS INT)'
SET #sql = 'SELECT #fianalValue = '+CAST(#value AS NVARCHAR(100))+''
exec sp_executesql #sql, N'#fianalValue INT OUTPUT', #fianalValue = #fianalValue OUTPUT
PRINT '#fianalValue: ' + CAST(#fianalValue AS VARCHAR(50))
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));