I have the following stored procedure, it works fine except that when returns all the results it will start over and create another window, it is looping over and over creating table after table with the same results. What could be causing this?
USE [HRLearnDev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[COL_Run_DOM_Parameters]
#StartDate varchar (50),
#EndDate varchar (50)
AS
SET NOCOUNT ON
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN #StartDate and #EndDate
ORDER BY REC_EFF_STT_DT DESC
EXECUTE COL_Run_DOM_Parameters #StartDate = "2010-03-05", #EndDate = "2011-06-11"
I don't think you want to do this within your stored procedure (ie: REMOVE it):
EXECUTE COL_Run_DOM_Parameters #StartDate = "2010-03-05", #EndDate = "2011-06-11"
This indicates the procedure is calling itself recursively, over and over and over ...
(Unless this was mistakenly included, and was just shown as an example of how you WOULD call the stored proc)
Related
I have tried to make a procedure to insert a price:
create procedure prInsertPrice
#NuggetID varchar(5),
#Unit_Price money,
#Start_Date datetime,
#End_Date datetime
as
begin
DECLARE #date AS DATETIME
SET #date = GETDATE()
if
(
(#NuggetID like 'N[0-9][0-9]')
and
(#Unit_Price is not null)
and
(#Start_Date is not null)
)
begin
print 'Insert Success'
insert NuggetPrice (NuggetId, Unit_Price, Start_Date, End_Date)
values (#NuggetID, #Unit_Price, #Start_Date, #End_Date)
end
else
begin
print 'Failed to insert'
end
end
When I execute the procedure it's fine, but when I run the procedure like this:
EXEC prInsertPrice 'N01', 20000, #date, null
I get the error message:
Must declare the scalar variable #date.
Why is this and how can I correct the problem?
The #date in the exec statement is different then the one in the stored proc.
You should do something like:
DECLARE #date AS DATETIME
SET #date = GETDATE()
EXEC prInsertPrice 'N01', 20000, #date, null
When you run:
EXEC prInsertPrice 'N01', 20000, #date, null
You are passing the variable #date as the third parameter to your stored procedure, as #Start_Date. This is entirely separate from the #date variable which you have declared inside the stored procedure itself, which gets declared and initialised after the procedure has been called, as it executes.
If you have not initialised the #date variable which is being passed as a parameter to the stored procedure before calling the stored procedure, then you will get the error you have described.
So, you need to declare and initialise this variable first:
DECLARE #date DATETIME = '2017-01-01' -- You can whatever date value you require here
EXEC prInsertPrice 'N01', 20000, #date, null
This should prevent the error.
Note: You can also separate the declaration and initialisation of the #date variable if you would prefer:
DECLARE #date DATETIME
SET #date = '2017-01-01'
In addressing your underlying problem though or preventing bad data being inserted into your NuggetPrice table though, I would agree with Prdp's suggestion of adding a CHECK Constraint to the table, for example:
ALTER TABLE NuggetPrice
ADD CONSTRAINT CK_NuggetPrice CHECK (NuggetID LIKE 'N[0-9][0-9]'
AND Unit_Price IS NOT NULL
AND Start_Date IS NOT NULL)
This would also prevent anyone from inserting records which do not agree with the logic specified.
Already other two answers gave enough information on the reason for error so am not going to talk about it. Here is different approach to solve the data validation
Instead of creating a Stored Procedure to restrict inserting bad data into table, I would suggest you to create a Check constraint to do this
ALTER TABLE NuggetPrice
ADD CONSTRAINT CK_NuggetPrice CHECK (NuggetID LIKE 'N[0-9][0-9]' AND Unit_Price IS NOT NULL AND Start_Date IS NOT NULL)
This will make sure nobody inserts bad data in NuggetPrice
I'm running a simple while loop that executes a stored procedure given a condition. Is there any way to access the stored procedure variable and change a variable within it before it executes? The variable I want to change is the same as the #date variable.
Thanks for any help guys.
declare #dayscount int
Declare #date as date
set #dayscount = 256
set #date = cast(GETDATE() - #dayscount as Date);
while (#dayscount >= 1)
exec A
set #dayscount = #dayscount - 1
You can try recursion in a CTE if all you are doing is decrementing or dynamic sql within your stored procedure and the replace function to replace the variable with the values you want and then run the execute_sql stored procedure within your stored procedure.
I have a stored procedure that is run from a SSRS report, my problem now is that I need to include data from the previous year in the report. I wanted to be able to run the report once with the set of parameters an than a second time with the previous years data to include into my report to be able to compare the data line by line. What I have is creating an error. I am somewhat new at SQL Server, any help is much appreciated. This is built in SQL Server 2008
DECLARE
#StartDate datetime,
#EndDate datetime,
#iClientID int,
#iServiceLevelID int
SET #StartDate = '1-1-13'
SET #EndDate = '12-30-13'
SET #iClientID = null
SET DATEFIRST 7
DECLARE #DATA table(iclientID int,
sClientCode varchar(8),
sClientName varchar(50),
sServiceLevelName varchar(50),
DeailyProductionAverage float,
CorrectionPercentage float,
AverageAging float,
decProduction float,
EffectedDate datetetime,
RepID int,
FirstName varchar(50),
LastName varchar(50),
Completed float)
insert into #DATA
exec procSSRS_ClientPerformanceNew_2 #StartDate,
#EndDate,
#iClientID,
#iServiceLevelID
insert into #DATA
exec procSSRS_ClientPerformanceNew_2 dateadd(year, -1, #StartDate)
dateadd(year, -1, #Enddate)
#iClientID
#iServiceLevelID
You are missing the commas in your parameter list when you call the procedure second time. Change your code to this to make it work:
exec procSSRS_ClientPerformanceNew_2
dateadd(year,-1,#StartDate),
dateadd(year,-1,#Enddate),
#iClientID,
#iServiceLevelID
In SQL Server, when passing an argument to a stored procedure, the argument can be either a constant or a variable. It cannot be e.g. a mathematical expression, nor can it be a function call.
So, in order to call the procedure again with the different data range, you will need to store the results of dateadd calls to variables and use those as arguments. For instance, you could re-use #StartDate and #EndDate for that if their original values are not needed later:
...
set #StartDate = dateadd(year,-1,#StartDate);
set #EndDate = dateadd(year,-1,#Enddate);
insert into #DATA
exec procSSRS_ClientPerformanceNew_2 #StartDate,
#EndDate,
#iClientID,
#iServiceLevelID
;
or you could declare two more datetime variables.
I have one big SQL query and I want to pull out some data using that query
declare #Period VARCHAR(10) = 'MTD'
declare #Date DATETIME = '2011-08-31'
and I have a big select statement where I'm passing above parameters and it executes the output.
Now I have 10 different dates which I need to pass here each time to see the result.
How can I pass those date to above parameter declare #Date DATETIME how can I hard code it ?
So my desired output will be for those selected dates, give me hint for at least 3 dates ?
Use a table-valued parameter. First, create a type:
CREATE TYPE dbo.Dates AS TABLE(d DATE);
Now your stored procedure can take this type as a parameter:
CREATE PROCEDURE dbo.whatever
#d dbo.Dates READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT t.columns
FROM dbo.tableName AS t
INNER JOIN #d AS d
ON t.[date] = d.d;
END
GO
Then from your application you can pass this parameter in as a DataTable, for example.
I'm first time trying to write a stored procedure in sql server and the codes as below. Here when I add "with rollup" at the end of the query, it shows error "Incorrect syntax near the keyword with"
DROP PROCEDURE FIRSTPROCEDURE
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE FIRSTPROCEDURE
#startdate nchar(8), #enddate nchar(8)
AS
BEGIN
SET NOCOUNT ON;
select Date, SUM(QT1), SUM(QTY2), SUM(qTY3) FROM dbo.TABLE1
where date between #startdate and #enddate
group by Date
order by Date
WITH ROLLUP
END
GO
And tried to execute the procedure as below :
exec firstprocedure '20120501', '20120525'
With rollup needs to come before the order by. It's related to the group by
select Date, SUM(QT1), SUM(QTY2), SUM(qTY3) FROM dbo.TABLE1
where date between #startdate and #enddate
group by Date WITH ROLLUP
order by Date
Also, you'll avoid a whole bunch of issues if you store and query your dates using the date datatype