SQL Server 2012 stored procedure error Msg 102 - sql

I use SQL Server Management Studio 2012. When I try to create a stored procedure and I execute it, it returns an error every time. I really can not figure out whats wrong.
For example:
use AdventureWorks2012
CREATE PROCEDURE test () SELECT * FROM [Sales].[SalesPerson]
and the error is :
Msg 102, Level 15, State 1, Procedure test, Line 1
Incorrect syntax near ')'.
even If I just write CREATE PROCEDURE TEST
It will return error ? What am I doing wrong ?

Try this code:
CREATE PROCEDURE test
as
begin
SELECT * FROM [Sales].[SalesPerson]
end
go
You have to remove () if your procedure does not need parameters, and add as, optionally begin-end (it's more usable).
If you want to add parameters to your procedure in future:
CREATE PROCEDURE test
(
#param1 int
)
as
begin
SELECT * FROM [Sales].[SalesPerson]
where id = #param1
end
go

You should have AS keyword when creating procedure
use AdventureWorks2012
GO
CREATE PROCEDURE test
AS
SELECT * FROM [Sales].[SalesPerson]
GO
Additionally you can add BEGIN and END if you have multiple queries on it.

Related

How to create a stored procedure to copy data from a query to a temporary table?

I have need of inserting data to a temporary table from an existing table/query. The following produces the error detailed below.
CREATE TABLE SPTemporary
AS
BEGIN
SELECT * into #temppT
FROM SampleTable
END
Throws this error:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'begin'.
Correct your syntax, use procedure instead of table :
create procedure SPTemporay
as
begin
select * into #temppT
from SampleTable
end
However, if you want only copy of data then only subquery is enough :
select st.* into #temppT
from SampleTable st
One method is:
select st.*
into SPTemporay
from SampleTable st
One select can only put data in one table. It is unclear which one you really want SPTemporary or #temppT. You can repeat the select if you really need the same data in two tables.
EDIT:
If you want a stored procedure, you could do:
create procedure SPTemporay
as begin
select *
into #temppT
from SampleTable
end;
This is rather non-sensical, because the temporary table is discarded when the stored procedure returns.
I think the syntax is wrong, it should be like that:
create table SPTemporay
as
select * from SampleTable
I hope this helps.

Cannot create new procedure in SQL manager lite

I am working with SQL manager lite for Interbase/Firebird application. I have downloaded firebird database, successfully connected to that database and its host, but now I want to create procedure.
I couldn't done it via tutorials, so I decided to just click New->Procedure and do that automatically. But doing this way I still have errors.
My code what I have tried without clicking New->Procedure:
CREATE PROCEDURE MyProc
AS
SELECT M_DOKUMENTY.NDZIEN FROM M_DOKUMENTY WHERE M_DOKUMENTY.SRODZAJ = '1234'
GO;
The code which was generated using New->Procedure wizard:
CREATE PROCEDURE SHOW_ALL
AS
BEGIN
/* Procedure body */
SELECT
M_DOKUMENTY.NDZIEN,
M_DOKUMENTY.CKIERUNEK,
M_DOKUMENTY.CMEDIUM FROM M_DOKUMENTY WHERE M_DOKUMENTY.SRODZAJ = '1234'
SUSPEND;
END;
But when I am clicking that lightning icon (compile) it complains about error:
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 9, column 3.
SUSPEND.
How to fix that?
Screenshot of error in SQL Manager lite
The problem is that your syntax is wrong. You need to define the output parameters, and you need to use either select ... into <list of variables> to select a single row, or for select ... into <list of variables> do to loop over multiple rows.
Your stored procedure should be something like:
CREATE PROCEDURE SHOW_ALL
RETURNS (NDZIEN varchar(50), CKIERUNEK varchar(50), CMEDIUM varchar(50))
AS
BEGIN
/* Procedure body */
for SELECT
M_DOKUMENTY.NDZIEN,
M_DOKUMENTY.CKIERUNEK,
M_DOKUMENTY.CMEDIUM
FROM M_DOKUMENTY
WHERE M_DOKUMENTY.SRODZAJ = '1234'
into :NDZIEN, :CKIERUNEK, :CMEDIUM
do
SUSPEND;
END
If your select only produces a single row, then you could also consider using
CREATE PROCEDURE SHOW_ALL
RETURNS (NDZIEN varchar(50), CKIERUNEK varchar(50), CMEDIUM varchar(50))
AS
BEGIN
/* Procedure body */
SELECT
M_DOKUMENTY.NDZIEN,
M_DOKUMENTY.CKIERUNEK,
M_DOKUMENTY.CMEDIUM
FROM M_DOKUMENTY
WHERE M_DOKUMENTY.SRODZAJ = '1234'
into :NDZIEN, :CKIERUNEK, :CMEDIUM;
SUSPEND;
END
Notice the ; after the into clause. In this case you could also leave out the SUSPEND;. That will make the stored procedure executable instead of selectable. Depending on how you want to use it, that could be a better choice.
See the Firebird documentation on created stored procedures and its procedural SQL language for more information.

checking for existence of a stored procedure

Does any body know why the following SQL returns a syntax error (Incorrect syntax near 'IF') in SQL Server 2012?
DROP PROCEDURE IF EXISTS MyStoredProcedure;
I know I can use the following instead:
IF EXISTS(SELECT 1 FROM sys.procedures
WHERE Name = 'MyStoredProcedure')
But I wonder why the first one produces the error.
Thanks!
This works:
IF EXISTS(SELECT 1 FROM sys.procedures
WHERE Name = 'MyStoredProcedure')
print 'yes'
-- drop procedure mystoredprocedure
You must have something else in your query window that's throwing the error, or you're running things in the wrong order.
For what it's worth, this is my "go to" for dropping/building procedures before I CREATE PROCEDURE... all my saved proc scripts have this.
IF OBJECT_ID('dbo.uspSomeProcName') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.uspSomeProcName
IF OBJECT_ID('dbo.uspSomeProcName') IS NOT NULL
PRINT '<<< FAILED DROPPING PROCEDURE dbo.uspSomeProcName>>>'
ELSE
PRINT '<<< DROPPED PROCEDURE dbo.uspSomeProcName>>>'
END

CREATE PROCEDURE permission denied in database 'master'

I am creating procedure in sql server, My code is as follows
CREATE PROCEDURE GetRegistrationId
(
#MaxId INT OUTPUT
)
AS
BEGIN
SELECT #MaxId = MAX(UserId) FROM tblRegistration;
return;
END
but it gives error saying
CREATE PROCEDURE permission denied in database 'master'.
Try this:
USE <Your Database Name>
GO
CREATE PROCEDURE GetRegistrationId(#MaxId INT OUTPUT)
AS
BEGIN
SELECT #MaxId=MAX(UserId) FROM tblRegistration;
RETURN;
END
OR
Select "Your Database Name" from Toolbar (SQL Editor) and then Execute the procedure
Try below of any techniques.
On top of your create procedure statement write this USE [YOUR_DBNAME].
USE [YOUR_DBNAME]
GO
CREATE PROCEDURE GetRegistrationId
(
#MaxId INT OUTPUT
)
AS
BEGIN
SELECT #MaxId = MAX(UserId) FROM tblRegistration;
return;
END
or
In SQL Server, At your SQLQuery Editor choose your target database from available Database drop down list and execute your Stored Procedure.
Comment below if you still face any issue.
Switching context to your DB would be the best approach. Hard coding
USE <YourDB>
in the beginning of the procedure, or using a fully qualified name to include DB name will make the SP less portable

Reporting Services: Creating user defined functions in report query. Is it real?

Is it possible to create and use a user defined function in a report query?
Something like this:
if OBJECT_ID('userFunc', 'TF') is not null drop function userFunc
go
create function userFunc(#id int)
returns #tbl table
([id] int)
begin
insert into #tbl (id) values(#id)
return
end
go
select * from userFunc(1)
if OBJECT_ID('userFunc', 'TF') is not null drop function userFunc
When i run this query in SQL Server 2008 Management Studio it returns me 'id':1, but when i put this query into Reporting Query designer - i get the next error:
TITLE: Microsoft Report Designer
An error occurred while the query
design method was being saved.
Incorrect syntax near 'go'.
'CREATE FUNCTION' must be the first
statement in a query batch.
Incorrect syntax near 'go'.
Incorrect syntax near 'userFunc'.
Any suggestions? How to create and use udf in Reporting Services queries?
"GO" will only be recognised by SQL tools: not the DB engine nor SSRS parser
This might if you really want to
EXEC 'if OBJECT_ID(''userFunc'') is not null drop function userFunc'
EXEC 'create function userFunc(#id int)
returns #tbl table
([id] int)
begin
insert into #tbl (id) values(#id)
return
end'
select * from userFunc(1)
However, you'll need ddl_admin or db_owner to run it.
Have you considered a CTE or derived table? A table valued udf like this can be replaced by a CTE