Store Procedure Errors - sql

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;

Related

Create synonym for function

Would you prompt me, please, how to create synonym for function in SQL Server?
This method: CREATE SYNONYM cDateMin FOR server.db.[dbo].cDateMin
Doesn't help.
Script:
select dbo.cDateMin()
leads to the error:
Msg 4121, Level 16, State 1, Line 9
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.cDateMin", or the name is ambiguous.
Script:
select cDateMin()
leads to the error:
Msg 195, Level 15, State 10, Line 9
'cDateMin' is not a recognized built-in function name.
Script:
select dbo.cDateMin
leads to the error:
Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "dbo.cDateMin" could not be bound.
Script:
select cDateMin
leads to the error:
Msg 207, Level 16, State 1, Line 9
Invalid column name 'cDateMin'.
Guide from official source (to make a synonym by clicking the right button of the mouse on synonyms folder in SSMS (link) and creation the synonym 'by hand') created the same synonym and led tho the same errors.
Creation synonyms with the names different from the function name also didn't help.

How do I do select into from another table

Basically I want to run a SQL statement like this, but I can't due to errors.
Not sure why?
INSERT INTO allevent (eventname)
VALUES (select username from registered)
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
No need of parentheses around the SELECT statement in your query.
INSERT INTO allevent (eventname)
SELECT username FROM registered
Parentheses with VALUES required, where some places like below:
INSERT INTO tablename (fieldname)
VALUES ('field 01'), ('field 02');
Please find the syntax for the INSERT statement.

Create a table with SQL from Query result in SSMS

I am trying to create a new table from a query result in SSMS
I have tried:
CREATE TABLE table2 AS
(SELECT * FROM table1
WHERE code = 'x'
ORDER BY code;)
However I am getting an error telling me:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'ORDER'.
Can anyone help?
Try this:
SELECT *
INTO NEW_TABLE
FROM table1
WHERE code = 'x'
ORDER BY code;

sql error procedure

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]
(
.....

Understanding SSMS error messages, regarding an INSERT error:

When I run an SSMS command to insert data, I get this error:
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with ntext
What is ment by Level 16, State 2
My insert is a standard insert:
insert into surveys
values (18964, 'CD9E8ACE-0D22-4969-B324-620DAFF06F97', 19959, 486, etc etc
How do I find where my error was ?
Probably you are inserting value without single quote on a string column. String values need to be quoted as they are literal not identifiers.