Getting error Incorrect syntax near the keyword 'BEGIN' when executing stored procedure - sql-server

So, I'm new to MS SQL (have been using oracle for the last 5-7 years) and this should be very a straight forward thing to do, so I reckon I'm missing something very simple.
(I've tried following the examples here: http://technet.microsoft.com/en-us/library/ms190669(v=SQL.105).aspx)
So, I create the following stored procedure to query a table (this is a very simple and pointless procedure but I can't proceed with my more complex procedure until I resolve this problem)
create procedure sp_getTransactions
as
select * from MyTransactions;
I then try to execute this procedure
execute dbo.sp_getTransactions
(I've tried without the dbo. and get the same error)
This gives me the very helpful error
Incorrect syntax near the keyword 'BEGIN'.
Now, maybe I'm crazy but I don't see a begin statement anywhere in my procedure (I've tried adding one to no avail).
Can anyone give me some pointers here?
Thanks

Actually, the problem as it turns out is the client I was using. I was executing the sql scripts using Oracle's SQLDeveloper with the MSSQL jTDS driver. It seems this driver works fine for the most part, but when it comes to running stored procedures there's a bug.
I guess the execute statement isn't parsed properly by the plugin when being set to the server

Check like this:
CREATE PROCEDURE sp_getTransactions
AS
BEGIN
SELECT * FROM MyTransactions;
END
In execute dbo.sp__getTransactions statement, you used 2 _, but in CREATE PROCEDURE statement it has only one. Change to dbo.sp_getTransactions and try to execute.

Related

What is "dummy" in CREATE PROCEDURE statement

I'm doing investigation of code repo and find one thing that make me confused. SQL Server stored procedures are contained in a repo as a set of queries with following structure:
IF OBJECT_ID(N'[dbo].[sp_ProcTitle]', N'P') IS NULL
BEGIN
EXEC dbo.sp_executeSQL N'CREATE PROCEDURE [dbo].[sp_ProcTitle] AS dummy:;';
END
ALTER PROCEDURE dbo.sp_ProcTitle
#ParamOne int,
#ParamTwo date,
#ParamThree int
AS
SET NOCOUNT ON
-- some procedure body
END
Never before I saw AS dummy:; and now I'm a little confused, I can't find any good explanation what is it and how it works. Could anybody tell me what does it mean this statement? How it works? What is the reason to have it? Any thought would be good to hear. Or, please, advise me some link where I can find good explanation.
This is simply a label, such that could be used in a GOTO statement.
The word "dummy" is unimportant. It's simply trying to create the stored procedure if it doesn't exist, with a minimal amount of text. The content is then filled in with the ALTER.
Conceivably, the dummy text could later be searched for to see if any procedures were created and didn't have their content filled in, to check against failed deployments, etc.
Why do this? Well, it preserve the creation time of the stored procedure in metadata (which can be useful in administration or tracking down problems), and is compatible with versions of SQL Server that lack the CREATE OR ALTER... support.
This might make a little more sense if we add a little formatting to the CREATE:
CREATE PROCEDURE [dbo].[sp_ProcTitle]
AS
dummy:
This is, effectively, an empty procedure with a label called dummy. The user appears to be using this to ensure that the procedure exists first, and the ALTERing it. In older versions of SQL Server, such methods were needed because it didn't support CREATE OR ALTER syntax. As such, if you tried to ALTER a procedure that didn't exist the statement failed, and likewise if you try to CREATE a procedure that already exists it fails.
If you are on a recent version of SQL Server, I'd suggest changing to CREATE OR ALTER and getting rid of the call to sys.sp_executesql.

Rename of procedure does not reflected to code when i rename using GUI IN SQL Server

I have changed the name of a stored procedure in SQL Server using the GUI and it changed, but whenever I try to modify procedure code with sp_helptext, in the query editor, it was showing the old procedure name.
How to get rid of the problem?
It seems that this definition is being updated only when you run ALTER statement..
Do not use use sp_helptext for scripting!
If you want to modify the procedure code, use SSMS GUI: right click on procedure > alter to > new query.
Or use one the following to acquire the defition from T-SQL:
select object_definition(object_id('uspTestProc'))
select * from sys.sql_modules where object_id = object_id('uspTestProc')
Apart from the problem you're experiencing, in sp_help docs you can read about sp_help result: "Each row contains 255 characters of the Transact-SQL definition". This means that if you have lines longer than 255 character in procedure code, such line will be broken down into 255 char chunks. In best case scenario your code won't work, but if you're unlucky it will work and produce incorrect result, for example if some code that you had in a comment will be movde to new line and become uncommented.
Renaming Procedure names in SQL Server is not a good option as it does not update the procedure text.
Avoid renaming procedures and prefer DROP and CREATE.

Creating table and populating within stored procedure

My goal is to create a stored procedure that creates a new table and inserts the data from a long Select statement into said table. Seems fairly straightforward but I am getting an error at the END statement and cannot understand why.
Essentially the code is:
CREATE PROCEDURE TESTProc
AS
BEGIN
SELECT *
INTO NEW_TABLE
FROM (long select query)
END
However there seems to be an error right at the END:
Incorrect syntax near 'END'. Expecting AS, ID, or QUOTED_ID.
Is what I'm trying to do legal in a SQL Server stored procedure? As the select statement in the middle that populates works fine on its own, I thought maybe the method I was using to try and populate the newly created table is not the best way to do this?
Any advice or a way I can restructure this to work? Thanks!

Error /w OPENQUERY access error in SQL Server create procedure. Possible to create anyway?

Basic idea is I need to create a procedure, but I don't have access to the OPENQUERY linked server table so it errors out stating user does not have SELECT access. The idea is I'm trying to create the stored procedure and will have someone who does have access run it.
I've tried TRY/CATCH and that still provided the same issue. Is it due to the OPENQUERY? Any other way to ignore the error? Not sure why wrapping it in TRY/CATCH doesn't seem to ignore the error?
Basic code:
BEGIN TRY
SELECT *
FROM OPENQUERY(SERVER_NAME,'SELECT ABC FROM ABC.DEF')
END TRY
BEGIN CATCH
END CATCH
Was able to wrap the OPENQUERY /w EXEC instead which worked around this issue.

incorrect syntax near go

I used to write this on top of any create procedure statement. It worked fine when executed from either python sybase lib; c++ sybase lib, or Toad for sybase.
However since I started using "oracle sql developper" it is not working anymore. I get "incorrect syntax near go".
I can execute separately ( the below and the create procedure ) but not execute without the go, otherwise I get "create procedure statement must be the first statement of a batch"
IF EXISTS
(
SELECT 1
FROM sysobjects
WHERE type = 'P' and name = 'proc_name'
)
BEGIN
DROP PROCEDURE proc_name
END
GO
I cannot use toad as I am working with control freaks admin. I'm not local admin, I cannot install software the tools used are dictated by management.
Do you have any idea ?
This appears to be an issue related to SQLDeveloper.
REPLACE each GO by / and it runs.

Resources