Can't figure out Scalar Variable Error when Altering Procedure - sql-server

I have an existing sql server stored procedure, that I'm trying to update. Every time I run Alter Procedure on it, I get the following error:
SQL Error: Must declare the scalar variable "#varOne".
I've Googled around but nothing that I can find helps out with how to get past this error... :/ Procedure below:
ALTER PROCEDURE [dbo].[sp_test] (
-- Declare
#varOne tinyint,
#varTwo numeric(17,0)
) As
Set NoCount On
-- Select
Select *
Into #t
From SQL_Test.dbo.trans with (nolock)
Where test>=123
And Case #varOne When 1 Then rNo When 2 Then iNo When 3 Then sNo End = #varTwo
Union
Select *
From SQL_Test.dbo.trsave with (nolock)
Where date_time > DateAdd(dd, -60, GetDate())
And Case #varOne When 1 Then rNo When 2 Then iNo When 3 Then sNo End = #varTwo
EDIT: I solved the problem by dropping the original procedure, and recreating it. Still curious why the alter would not work. Guessing it is caused by the client using SQL Server 2012.

I solved the problem by dropping the original procedure, and recreating it. Still curious why the alter would not work. Guessing it is caused by the client using SQL Server 2012.

Related

Why is the table inside a non-met IF being validated before condition is met, resulting in error if table does not exist?

I am trying to execute a procedure with a parameter, and depending on the value of the parameter, three different IF conditions will be evaluated to verify which query it will execute from a linked server.
But when I execute the query, it seems to be checking if the tables inside all the IF exists before starting the query. And I know that only one of the table exists, that is why I am using the parameter, so it shouldn't fail. but I anyhow get the following error:
Msg 7314, Level 16, State 1, Line 25
The OLE DB provider "Microsoft.ACE.OLEDB.16.0" for linked server "LinkedServer" does not contain the table "D100". The table either does not exist or the current user does not have permissions on that table.
So in this code, assume that the parameter is 300. then I get the message above.
Do you know, if there is a way, to limit the query to do not check all the tables, but only the one where the IF condition will be met?
ALTER PROCEDURE[dbo].[Import_data]
#p1 int = 0
AS
BEGIN
SET NOCOUNT ON;
IF(#p1 = 100)
BEGIN
DROP TABLE IF EXISTS Table1
SELECT [Field1], [Field2], [Field3], [Field4], [Field5], [Field6]
INTO Table1
FROM[LinkedServer]...[D100]
END
IF(#p1 = 200)
BEGIN
DROP TABLE IF EXISTS Table2
SELECT[Field1], [Field2], [Field3], [Field4], [Field5], [Field6]
INTO Table2
FROM[LinkedServer]...[D200]
END
IF(#p1 = 300)
BEGIN
DROP TABLE IF EXISTS Table3
SELECT[Field1], [Field2], [Field3], [Field4], [Field5], [Field6]
INTO Table3
FROM[LinkedServer]...[D300]
END
END
I have tried googling it, but I found mostly workarounds as running a sub procedure, but it is not really a clean solution, I think.
Okay, it seems I that I found the answer. Even with an IF statement, the SQL Server validates the entire query before executing it, so the way to overcome it, is to use a Dynamic SQL Query.
"SQL Server Dynamic SQL is a programming technique that allows you to construct SQL statements dynamically at runtime. It allows you to create more general purpose and flexible SQL statement because the full text of the SQL statements may be unknown at compilation."
This is how the query looks now. so instead of multiple IF statements, the query changes dynamically depending on the parameter.
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = N'DROP TABLE IF EXISTS Table1;
SELECT [Field1]
,[Field2]
,[Field3]
,[Field4]
,[Field5]
,[Field6]
INTO Table1
FROM [LinkedServer]...[D' + CONVERT(nvarchar(3),#p1) + N']'
EXEC sp_executesql #SQL

Why #temptable instead #tempTable causing a timeout error?

I have a stored procedure called myStoredProcedure in SQL Server 2008 including a code block like this:
...
declare #tempTable table
(
Id int,
Name varchar(100),
Category varchar(50),
Volume int
)
while #startTime<#endTime
begin
insert into #tempTable
EXEC R52_Calculations #param1, #param2, #param3
set #startTime = DATEADD(YEAR,1,#startTime)
end
select * from #tempTable
In this way, the stored procedure is working very well. I can connect to a table to this stored procedure in the SSRS 2008 without any warning or error. However, when I change #tempTable variable into #tempTable like below, I am getting a TimeOut error when I try to connect a table on SSRS 2008 to the updated stored procedure, even though the stored procedure is working very well again in SQL Server.
...
create table #tempTable
(
Id int,
Name varchar(100),
Category varchar(50),
Volume int
)
while #startTime<#endTime
begin
insert into #tempTable
EXEC R52_Calculations #param1, #param2, #param3
set #startTime = DATEADD(YEAR,1,#startTime)
end
select * from #tempTable
This is the error:
Timeout expired. The timeout period elapsed prior to completion of
the operation or the server is not responding. Warning: Null value is
eliminated by an aggregate or other SET operation.
Some more points:
when I remove the "while" loop and do the process only 1 time, there is no error occurring when I use #tempTable.
if I use #temptable (variable one), there is no error occurring at all.
Note that both queries are working fine in SQL Server, the errors are occurring when I try to connect a table on SSRS 2008 to the stored procedure
.
I could not find the reason why #temptable is causing an error. Any clue or help I will appreciate. Thanks.

Error:create function must be the only statement in the batch

I am trying to firstly check if the function exists then create it, if it doesn't exist.
I'm getting this error from the function:
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[GetRelativeExpiry]') AND type in (N'U'))
BEGIN
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE FUNCTION [dbo].[GetRelativeExpiry]
(
#Date DATE,
#N INT
)
RETURNS DATE
AS
BEGIN
-- Declare the return variable here
DECLARE #Expiry as DATE;
IF #N > 0
BEGIN
SELECT #Expiry = MAX(E2.Expiry)
FROM (SELECT TOP(#N) Expiry
FROM ExpiryDates E1
WHERE E1.Expiry >= #date
ORDER BY E1.Expiry) AS E2
END
ELSE
BEGIN
SELECT #Expiry = MIN(E2.Expiry)
FROM (SELECT TOP(-#N) Expiry
FROM ExpiryDates E1
WHERE E1.Expiry <= #date
ORDER BY E1.Expiry DESC) AS E2
END
RETURN #Expiry
END
END
I am not sure why I am getting this error, could someone please help?
I am using Microsoft SQL Server Management Studio 2014
CREATE statements, whether they are for TYPE, PROCEDURE, FUNCTION, ... should always be the first statement in a batch.
To work around this in batches like yours execute the CREATE statement using sp_executesql like this:
EXEC sp_executesql N'
-- your CREATE statement here
';
If you are trying to do this in regular window within MS SQL Server Management Studio 2014 (or any prior version), where you would normally write all other queries, then your definition of function must be very first statement that is not commented.
See image below. If statement USE PTCRMstaging_test; was not commented, SQL Server Management Studio would give that same error.

IsNull behaves differently between Sybase 12.5 and 15.7

I am upgrading an application to connect to Sybase ASE 15.7 database servers instead of 12.5. When I switched it over, a stored procedure's behavior changed which caused problems with the application. I narrowed the cause down to the IsNull function being called inside the procedure.
I found that running IsNull on its own worked fine on 15.7:
select IsNull((SELECT 9 WHERE 5 != 5),-1) -- returns -1
However, trying to assign the return value to a variable did not work:
DECLARE #key_clnt_id_n int
SELECT #key_clnt_id_n = IsNull((SELECT 9 WHERE 5 != 5),-1)
select #key_clnt_id_n -- returns blank
Adding a 'from table' clause makes it work:
declare #eff_d datetime
select #eff_d = IsNull((select '09/09/1990' from db_table db WHERE 5!=5),'01/01/1800')
select #eff_d -- returns '01/01/1800'
But removing the 'from table' clause breaks it:
declare #eff_d datetime
select #eff_d = IsNull((select '09/09/1990' WHERE 5!=5),'01/01/1800')
select #eff_d -- returns blank
Finally, turning on COMPATIBLITY_MODE before executing also fixes it:
declare #eff_d datetime
SET COMPATIBILITY_MODE ON
select #eff_d = IsNull((select '09/09/1990' WHERE 5!=5),'01/01/1800')
SET COMPATIBILITY_MODE OFF
select #eff_d -- returns '01/01/1800'
What is going on in the 15.7 databases that causes this? Is the query in the IsNull causing the whole statement to short-circut and not assign a value to the variable?
Are there other ways to ensure the variable will get set?
Our DBAs worked with Sybase and this issue has been logged as a bug by Sybase: CR 742233, “Queries performing isnull() on a variable assignment may return null when ‘streamlined dynamic SQL’ is enabled.”
The workaround is to disable “streamlined dynamic SQL”.

Parametrized stored procedure error

I am using SQL Server 2005 Management Studio Express. Coins and themes are my tables. I created a stored procedures using the above two and got struck with
Error:Msg 102, Level 15, State 1, Procedure themestat, Line 1
Incorrect syntax near 'id2'.
Here is my whole procedure:
create procedure themestat(id2 In numeric, id1 In numeric)
is
#userid nvarchar(50), #co nvarchar(50), #price nvarchar(50)
begin
update themes set prioirty=1 where themeid=id2;
select credits as co from coins where uid=id1;
select rate as price from themes where priority=1;
if(co>price)
begin
update themes set status=1 where priority=1;
update themes set priority=0 where themeid=id2;
end
else
begin
update themes set priority=0 where theme=id2;
PRINT 'no sufficient coins'
end
end
I am curious to know where I went wrong ??
I'm not sure where you've got the syntax from, but datatypes are declared as '#param type', so the first line should read:
create procedure themestat
#id2 numeric
#id1 numeric
Then obviously change all references of id1 and id2 as appropriate. There's other syntax errors in the script (missing declare, is instead of as, possibly others - I've not looked much closer).
This makes me wonder whether you've come from a different SQL dialect? I suggst reading about CREATE PROCEDURE on the MSDN (as well as other pages).

Resources