What's wrong with this code?
CREATE PROCEDURE Proc
(
#factura_id int, #produs_id int, #pret float, #cantitate int,#nr_ordine int
)
as
--declare #factura_id int, #produs_id int, #nr_ordine int, #pret float, #cantitate int
begin
if(((select COUNT (id_produs) from Produse where id_produs=#produs_id)=1))
insert into FacturaProdus(id_factura,id_produs,pret,cantitate,nr_ordine)
values(#factura_id,#produs_id,#pret, CONCAT ('-',convert(float,#cantitate),#nr_ordine))
else
begin
print 'hei'
end
end
I can't find a solution for this.When i execute it, it gives me:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Proc'.
Msg 137, Level 15, State 2, Line 8
Must declare the scalar variable "#produs_id".
Msg 137, Level 15, State 2, Line 9
Must declare the scalar variable "#factura_id".
What to do?
Proc is reserved word in SQL server (I assume you're using it based on syntax and error messages).
So if you really want to create procedure having such a name (I recommend you to choose another name, though) - enclose it into square brackets:
CREATE PROCEDURE [Proc]
(
.....
Related
I am trying to make a stored procedure that uses a table as input parameter. However it is not working as intended. Steps I've taken:
First I make a user defined table type:
CREATE TYPE y_yhat_tabletype AS TABLE
(
UTC_DT datetime,
y float,
yhat float
);
Then I try to make a stored procedure:
create procedure sp_evaluate_y_yhat
#data y_yhat_tabletype readonly
as
begin
select sum(#data.y) as sum_y, sum(#data.yhat) as sum_yhat
end
But I get the error:
Msg 137, Level 16, State 1, Procedure sp_evaluate_y_yhat, Line 12 [Batch Start Line 0]
Must declare the scalar variable "#data".
Msg 137, Level 16, State 1, Procedure sp_evaluate_y_yhat, Line 12 [Batch Start Line 0]
Must declare the scalar variable "#data".
The select sum(#data.y) as sum_y, sum(#data.yhat) as sum_yhat line is just to get some output while testing and will be replaced with more operations as soon as I get this to work.
Any idea on what I'm missing?
#data is essentially a table, so you need to treat it as such.
Declare as:
create procedure sp_evaluate_y_yhat
(
#data y_yhat_tabletype readonly
)
as
begin
select sum(y) as sum_y, sum(yhat) as sum_yhat
from #data
end
go
It's worth noting that if #data contained many rows, the row estimate might be off as table variables don't get statistics.
DECLARE UserIdToUpdate
SELECT UserIdToUpdate = customer_id
FROM dbo.orders
I get this error message (translated from the Spanish version):
Msg 137, Level 15, State 1, Line 13
You must declare the scalar variable '#UserIdToUpdate'
You need first to decalre the user defined varoable before using it
CREATE tABLe orders( customer_id INTEGER)
GO
DECLARE #UserIdToUpdate INTEGER
SELECT TOP 1 #UserIdToUpdate = customer_id FROM dbo.orders
GO
db<>fiddle here
Whenever I try to create table in my selected database I get
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'
Can anybody tell me whats the error and how to overcome.
What's the error in it ?
CREATE TABLE first_name
(
)
The error is that every table needs to have at least a single column.
CREATE TABLE users
(
id int,
firstname varchar(255),
lastname varchar(255)
)
create procedure usbinsertbookDatainto
#Bookno Numeric,
#studentno Numeric,
#currentnoofcopiesavaillable varchar(300),
#Issuedate datetime,
#Duedate datetime
AS
BEGIN
Insert into tbluseraccount
(
Bookno,
studentno,
currentnoofcopiesavaillable,
Issuedate,
Duedate
)
values
(
#Bookno,
#studentno,
#currentnoofcopiesavillable,
#Issuedate,
#Duedate
)
END
The error I get is:
Msg 156, Level 15, State 1, Procedure
usbinsertbookDatainto, Line 8
Incorrect syntax near the keyword
'AS'.
Msg 137, Level 15, State 2,
Procedure usbinsertbookDatainto, Line
22
Must declare the scalar variable
"#currentnoofcopiesavillable".
remove the first occurrence of "(" and
remove the last "," from your list of vars
create procedure usbinsertbookDatainto
#Bookno Numeric,
#studentno Numeric,
#currentnoofcopiesavaillable varchar(300),
#Issuedate datetime,
#Duedate datetime
AS
BEGIN
Insert into tbluseraccount
(
Bookno,
studentno,
currentnoofcopiesavaillable,
Issuedate,
Duedate
)
values
(
#Bookno,
#studentno,
#currentnoofcopiesavillable,
#Issuedate,
#Duedate
)
END
It's just a typo: in your parameter list for the stored proc you have:
create procedure usbinsertbookDatainto
...
#currentnoofcopiesavaillable varchar(300),
and in the code you use:
values(.....
#currentnoofcopiesavillable,
So to make it clearer:
Defined: #currentnoofcopiesavaillable
Used: #currentnoofcopiesavillable
Make sure to type the same variable name as you've used in the declaration!
I have created a stored procedure and am receiving these errors. How do I rectify this?
Msg 156, Level 15, State 1, Procedure usbinsertbookDatainto, Line 8
Incorrect syntax near the keyword 'AS'.
Msg 137, Level 15, State 2, Procedure usbinsertbookDatainto, Line 22
Must declare the scalar variable "#currentnoofcopiesavillable".
We would have to see the actual stored procedure SQL to be sure, but the errors are as follows:
Near the AS keyword, you have mistyped / have invalid syntax near Line 8
You have not DECLAREd #currentnoofcopiesavillable. Example being:
DECLARE #currentnoofcopiesavillable int;