I have a stored procedure that I need pass the parameter from one to the other procedure and have it display as an output. I am declaring the following in the header of my procedure [xxx].[zzzz_ERP_Cyyyyy]
DECLARE #ProcedureLogRowKey INT
DECLARE #ProcedureRecordCount INT
DECLARE #ProcedureStartDateTime DATETIME
DECLARE #ProcedureLog_Note NVARCHAR(100)
EXEC [XXX].[spciProcedurePerformanceStartRecord_help]
'.[xxx].[zzzz_ERP_Cyyyyy]',
1,
#ProcedureStartDateTime,
'Contract Check',
#ProcedureLogRowKey OUTPUT
I am getting the following error:
Msg 515, Level 16, State 2, Procedure spciProcedurePerformanceStartRecord_help, Line 33 [Batch Start Line 17]
Cannot insert the value NULL into column 'YSTRTDTT_0', table '000.xxx.YPERLOG'; column does not allow nulls. INSERT fails.
Here is the procedure that I am getting the variable from to pass into my procedure [xxx].[zzzz_ERP_Cyyyyy]
CREATE PROCEDURE [xxx].[spciProcedurePerformanceStartRecord_help]
(#ProcedureName VARCHAR(200),
#ProcedureRecordCount INT = 1,
#ProcedureStartDateTime DATETIME = GETDATE,
#ProcedureLog_Note NVARCHAR(100),
#ProcedureLogRowKey INT OUTPUT --- I am passing this into my proc and
displaying it as output
)
AS
BEGIN
-- Set Default return for #ProcedureLogRowKey, used if logging is not turned on.
SET #ProcedureLogRowKey = -1;
-- Check to see if performance logging is enabled
IF EXISTS(SELECT ROWID FROM LIVE.YPERCON
WHERE YPROCNM_0 = #ProcedureName AND YLOGENA_0 = 2)
BEGIN
INSERT INTO xxx.YPERLOG (YROWKEY_0, YPROCNM_0, YRECCNT_0, YSTRTDTT_0, YENDDTT_0, YLOGNOTE_0,
YDURMS_0, CREDATTIM_0, UPDDATTIM_0, AUUID_0, CREUSR_0, UPDUSR_0)
SELECT
ISNULL(MAX(YROWKEY_0), 0) + 1,
#ProcedureName, #ProcedureRecordCount, #ProcedureStartDateTime,
'1753-01-01',
#ProcedureLog_Note, 0,
GETDATE(), GETDATE(), NEWID(), 'admin', 'admin'
FROM
xxx.YPERLOG
SELECT #ProcedureLogRowKey = ISNULL(MAX(YROWKEY_0), 0)
FROM xxx.YPERLOG
END
ELSE
BEGIN
DECLARE #Count integer
SELECT #Count = COUNT(0)
FROM LIVE.YPERERR
WHERE YPROCNM_0 = #ProcedureName
IS ISNULL(#Count, 0) = 0
INSERT INTO LIVE.YPERERR (YPROCNM_0, YREQDT_0, YLASTDT_0, YERRMSG_0,
CREDATTIM_0, UPDDATTIM_0, AUUID_0, CREUSR_0, UPDUSR_0)
VALUES (#ProcedureName, GETDATE(), '1753-01-01', 'Controller not defined or active',
GETDATE(), GETDATE(), NEWID(), 'admin', 'admin')
ELSE
UPDATE xxx.YPERERR
SET YLASTDT_0 = GETDATE()
WHERE YPROCNM_0 = #ProcedureName
END
END
Thanks in advance.
The issue is in procedure [xxx].[spciProcedurePerformanceStartRecord_help] with parameter #ProcedureStartDateTime DATETIME. You should set its default value this way:
In declaration set default value as NULL
#ProcedureStartDateTime DATETIME = NULL
It would look like tihs
CREATE PROCEDURE [xxx].[spciProcedurePerformanceStartRecord_help]
(
#ProcedureName VARCHAR(200)
,#ProcedureRecordCount INT = 1
,#ProcedureStartDateTime DATETIME = NULL
,#ProcedureLog_Note NVARCHAR(100)
,#ProcedureLogRowKey INT OUTPUT
)
AS
BEGIN
-- procedure's body
END
Inside procedure, at the beginning, check if #ProcedureStartDateTime parameter's value is NULL and if it is, set its value to GETDATE().
SET #ProcedureStartDateTime = ISNULL(#ProcedureStartDateTime, GETDATE())
You have declared DECLARE #ProcedureStartDateTime DATETIME and did not set any value to it. so, it is having NULL value and you are passing NULL value to the procedure execution
EXEC [XXX].[spciProcedurePerformanceStartRecord_help]
'.[xxx].[zzzz_ERP_Cyyyyy]',
1,
#ProcedureStartDateTime, -- NULL value passed here
'Contract Check',
#ProcedureLogRowKey OUTPUT
As the target column 'YSTRTDTT_0', table '000.xxx.YPERLOG', does not allow NULLs, you are getting error.
Related
I'm trying to take the ID of a new record inserted into TABLE A and use it in a subsequent insert.
But I'm getting an error saying that the newUserId variable isn't declared.
it's actually a table variable.
The code looks like this;
USE Acme;
GO
DECLARE #userPrincipalName VARCHAR(100),
#displayName VARCHAR(100),
#domainName VARCHAR(100),
#tId INT,
#uname VARCHAR(100);
DECLARE #newUserid TABLE (
id INT
);
-- FILL ME IN
SET #domainName = 'mydomain.org';
SET #userPrincipalName = 'ppan#mydomain.org';
SET #displayName = 'Pan, Peter';
SET #tId=4;
SET #uname = 'ppan';
INSERT INTO dbo.User
(column list)
OUTPUT Inserted.ID INTO #newUserId
SELECT '', #domainName, getutcdate(), #userPrincipalName, #displayName, other fields
-- Create New Profile Using NewID ** THIS IS WHERE IT DIES
INSERT INTO dbo.UserProfile
SELECT #newUserId.id,
'{}', GETDATE(), getdate(), ''
The specific error is:
8:55:51 AMStarted executing query at Line 1
Commands completed successfully.
8:55:51 AMStarted executing query at Line 3
Msg 137, Level 16, State 1, Line 36
Must declare the scalar variable "#newUserid".
Total execution time: 00:00:00.017
I've abbreviated the code for the sake of this post but line 36 is where I'm referencing SELECT #newUserId.id
Any tips would be appreciated.
Thanks
Because #newUserId is a table variable you can't select it as a variable.
you can try to use INSERT INTO ....SELECT ... FROM
INSERT INTO dbo.UserProfile
SELECT id, '{}', GETDATE(), GETDATE(), ''
FROM #newUserId
I coded a stored procedure called vc_FinishVidCast that accepts an int as an input parameter that will be a vc_VidCastID that we will need to mark as finished. The act of finishing a VidCast means we must change its EndDateTime to be the current Date and Time (think GetDate()) and change the vc_StatusID to the vc_StatusID for the ‘Finished’ status.
alter procedure vc_FinishVidCast
(#vidCastID int, #finished int)
as
begin
update vc_VidCast
set vc_StatusID = #finished
where vc_VidCastID = #vidCastID
end
go
exec vc_FinishVidCast '859', '2'
DECLARE #newVC INT
INSERT INTO vc_VidCast (VidCastTitle, StartDateTime, ScheduleDurationMinutes, vc_UserID,vc_StatusID)
VALUES ('Finally done with sprocs', DATEADD(n, -45, GETDATE()), 45,
(SELECT vc_UserID FROM vc_User WHERE UserName = 'tardy'),
(SELECT vc_StatusID FROM vc_Status WHERE StatusText='Started')
)
SET #newVC = ##identity
SELECT *
FROM vc_VidCast
WHERE vc_VidCastID = #newVC
EXEC vc_FinishVidCast #newVC
SELECT * FROM vc_VidCast WHERE vc_VidCastID = #newVC
I get an error:
Msg 201, Level 16, State 4, Procedure vc_FinishVidCast, Line 179
Procedure or function 'vc_FinishVidCast' expects parameter '#finished', which was not supplied.
You may want to try something like below:
DECLARE #Finished_ID INT
SELECT #Finished_ID = vc_StatusID FROM vc_Status WHERE StatusText='FInished'
EXEC vc_FinishVidCast #newVC,#Finished_ID
I created a function, now rather passing static value I want to add parameter in the function but after calling function it start throwing an error:
Procedure or function dbo.hello has too many arguments specified.
Function :
Create Function dbo.hello
(#InputstartDate Date, #InputendDate Date)
Returns #attendanceTemp table(STUD_NAME VARCHAR(50),
ATTD_DATE DATE ,
attd_DATEs DATE,
Attendance VARCHAR(20))
As
Begin
Declare #startDate DATE
SET #startDate = #InputstartDate
Declare #endDate Date
SET #endDate = #InputendDate
Declare #dateDifference INT
SET #dateDifference = DATEDIFF(day, #startDate,#endDate) ;
Declare #count INT
SET #count = 0
DECLARE #myTable TABLE (STUD_ID int,
countdd int,
STUD_NAME varchar(50),
AttDate Date
)
While #count <= #dateDifference
Begin
Insert Into #myTable (STUD_ID, countdd, STUD_NAME, AttDate)
Values (1, 123, 'HAIDER', #startDate)
Set #count = #count +1
Set #startDate = DATEADD(day, 1, #startDate)
End
Insert Into #attendanceTemp
Select
tb.STUD_NAME, ATTD_DATE, tb.AttDate,
Case
When att.DETAIL Is Null
Then 'ABSENT'
When att.DETAIL = 'ATTENDACE'
Then 'PRESENT'
End As Attendance
from
#myTable tb
Left Join
ATTENDANCE att on tb.AttDate = att.ATTD_DATE
Where
att.STUD_ID = 1 or att.STUD_ID IS NULL
Return
END
Calling the function:
select *
from dbo.hello('2014-04-01', '2014-04-10');
Error:
Procedure or function dbo.hello has too many arguments specified
Possibly you first created the function with only one parameter.
Then made changes to the 'create function' script, and forgot to deploy?
I would;
1. DROP FUNCTION dbo.hello
2. CREATE FUNCTION dbo.hello, with you script
3. Try executing your function again.
The function seems to work fine (Though I cannot run a full test due to not having table 'ATTENDANCE')
I need help to fix a type conversion issue that is bugging me for few hours on the sp I wrote. I want to split up bill number like this '12/SH/IFCR/7' and get the last int value and store it separately
Eg. '12/SH/IFCR/7' --> 12/SH/IFCR/ and 7
Note: 12/SH/IFCR/ is prefix which stays the same but the last number changes
ALTER PROCEDURE spGenerateCreditInvoiceForApi
#ShopId as int,
#TransId as int
--#CompanyId as int
AS
BEGIN
SET NOCOUNT ON;
declare #CompanyId as int
declare #Prefix as varchar(50)
declare #ProformaId as int
declare #MaxId as int
declare #FinId as int
declare #InvoiceNo as varchar(150)
set #CompanyId=(select CompanyID from aShops where ShopID=#ShopId)
set #FinId=(Select financialid from afinancialyear where Curfinancialyear = 1 and companyid = #CompanyId)
set #Prefix=(SELECT Prefix FROM aPrefix WHERE InterfaceID = 1504 and ShopId=#ShopId and FinancialId = #FinId)
set #ProformaId=(select ISNULL(MAX(CONVERT(INT,REVERSE(LEFT((REVERSE(ihInvoiceNo)),(PATINDEX('%/%' ,(REVERSE (ihInvoiceNo))))-1)))),0)
from LOsInvoiceHeader
where ihInvoiceID= #TransId and ihShopID=#ShopId)
--SET #intBillID = (SELECT CASE WHEN COUNT(poshBillid)=0 THEN 1 ELSE MAX(poshBillid)+1 END FROM losposheader WHERE poshShopID=#intShopId)
set #MaxId=(SELECT CASE WHEN COUNT(ihInvoiceNo)=0 THEN 1
ELSE MAX(ihInvoiceNo)+1 END
from losinvoiceheader
where ihShopId =#ShopId and ihfinancialid=#FinId and ihType='I')
SET #InvoiceNo = (#Prefix+CONVERT(VARCHAR,#MaxId))
--update LOsInvoiceHeader set ihInvoiceNo=#InvoiceNo, ihProformaID=#ProformaId where ihInvoiceID=#TransId and ihShopID=#ShopId
--print #InvoiceNo
END
GO
Error:
Msg 245, Level 16, State 1, Procedure spGenerateCreditInvoiceForApi, Line 33
Conversion failed when converting the varchar value '12/SH/IFCR/7' to data type int.
Thanks in advance.
How about something like this
declare #bill varchar(50),
#reversebill varchar(50),
#reverseResult varchar(50)
select #bill = '12/SH/IFCR/73'
select #reversebill = REVERSE(#bill)
select #reverseResult = SUBSTRING(#reversebill,0,CHARINDEX('/',#reversebill))
select Reverse(#reverseResult)
Thank god I found the issue
I changed use of Max() to count() and added a convert method to the whole query
Before
set #MaxId=(SELECT CASE WHEN COUNT(ihInvoiceNo)=0 THEN 1
ELSE MAX(ihInvoiceNo)+1 END
from losinvoiceheader
where ihShopId =#ShopId and ihfinancialid=#FinId and ihType='I')
After
set #MaxId=CONVERT(INT,(SELECT CASE WHEN COUNT(ihInvoiceNo)=0 THEN 1
ELSE CONVERT(INT,COUNT(ihInvoiceNo)+1) END
from losinvoiceheader
where ihShopId =#ShopId and ihfinancialid=#FinId and ihType='I'))
Hi i struggle with my stored-procedure which adds 'NULL' instead of a number
So why does the following procedure adds 'NULL' instead of a value between 0 and infinity?
Here is my procedure
ALTER PROCEDURE [dbo].[Plan_Abschluss]
-- My parameters for the stored procedure
#date AS datetime2(7),
#Einrichtung AS Int,
#Mitarbeiter AS Int
AS
BEGIN
-- declare my parameters
DECLARE #PlanStunden AS decimal(18, 2)= null,
#PlanUrlaub AS Int= null,
#oldDate AS datetime2(7)= null,
#oldUrlaubskonto AS Int= null,
#oldStundenKonto AS decimal(18, 2)= null;
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- set the previous month
SET #oldDate= DATEADD(month, -1, #date);
-- get some values from the previous row and set it to my parameters
SELECT #oldUrlaubskonto = ISNULL(CurrentUrlaubskonto,0) ,
#oldStundenKonto = ISNULL(CurrentStundenKonto,0)
FROM [Plan]
WHERE [Jahr] = YEAR(#oldDate)
AND [Monat] = MONTH(#oldDate)
AND [RefMitarbeiterId] = #Mitarbeiter
AND [RefEinrichtungId] = #Einrichtung;
-- get some values from the row i want to update and set it to my parameters
SELECT #PlanStunden = ISNULL(PlanStunden,0) ,
#PlanUrlaub = ISNULL(PlanUrlaub,0)
FROM [Plan]
WHERE [Jahr] = YEAR(#date)
AND [Monat] = MONTH(#date)
AND [RefMitarbeiterId] = #Mitarbeiter
AND [RefEinrichtungId] = #Einrichtung;
-- update the row and do a calculation with my parameters
UPDATE [Plan]
SET Abgeschlossen = 1,
CurrentUrlaubskonto = #oldUrlaubskonto+ #PlanUrlaub,
CurrentStundenKonto = #oldStundenKonto+ #PlanStunden
WHERE [Jahr] = YEAR(#date)
AND [Monat] = MONTH(#date)
AND [RefMitarbeiterId] = #Mitarbeiter
AND [RefEinrichtungId] = #Einrichtung
END
Variables will not be set in a select if no rows are returned. My guess is that the first select using #OldDate simply doesn't match any rows.
In particular, the variables #oldUrlaubskonto and #oldStundenKonto are initialized to NULL, so they are never set, when there is no matching record. One easy way to fix this is to use aggregation -- you are expecting one row anyway, so that is okay:
SELECT #oldUrlaubskonto = ISNULL(max(CurrentUrlaubskonto), 0) ,
#oldStundenKonto = ISNULL(max(CurrentStundenKonto), 0
You can also set the value afterwards, if it is still NULL.