Incorrect syntax near the keyword 'begin' in sybase - sybase

Here is my piece of code:
CREATE PROCEDURE my_procedure
BEGIN
EXECUTE IMMEDIATE '
CREATE VIEW abl_uds_bod AS
SELECT DISTINCT BOD_INTRMDTRY_ID, BOD_BROKERAGE
FROM BOD_BNFC_OWNR_DTLS
UNION ALL
SELECT DISTINCT UDS_CA_ID, UDS_SEC_ID
FROM UDS_UPLD_DSCLSR
UNION ALL
SELECT DISTINCT ABL_ORDR_ID, ABL_ERR_DESC
FROM ABL_ADD_BLK_LST
'
END
This is throwing error "Incorrect syntax near the keyword 'begin' ".
I have seen many people searching for the same problem but haven't got any satisfactory answer.

Assuming this s Sybase ASE, you are missing the AS keyword:
CREATE PROCEURE myproc
AS
BEGIN
....

Related

Executing stored procedure returns syntax error but running query works

I have a stored procedure that returns a syntax error when executed. However running the query on SQL Server Management studio works.
The syntax error is:
Msg 102, Level 15, State 1, Line 96
Incorrect syntax near 'se'.
The full stored procedure is here:
https://pastebin.com/nnQ65KPM
I have narrowed the problem to the last CTE. When this block is removed the stored procedure executes fine. All CTEs used in WellTestDetails_CTE work fine as well.
WellTestDetails_CTE (testmonth, well, result) as
(
select
b.month as testmonth, a.well, a.result
from
(select
well, result, result_no
from
(select * from WellTest1
union
select * from WellTest2) a
join
[Digital_Ecosystem_DEV].[dbo].[OrgAssigments] b on a.SUBFACILITY = b.SubFacility
where
b.team = ''X'') x
join
WellTestGrouped_CTE y on x.result_no = y.result_no
)
select *
from WellTestDetails
I think #SQL variable in DECLARE #SQL nvarchar(4000) is too small for all the dynamic SQL, try DECLARE #SQL nvarchar(MAX).

Dynamic SQL server query with variables (Problems executing)

This is a far smaller version of a query that basically needs a variable of the table on which everything is run.
When I run the procedure I get the error message:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'table'.
alter procedure james_tester
#tablename nvarchar(200)
as
BEGIN
declare #sql nvarchar(max)
set #sql =
'
select * from '
+ #tablename
EXECUTE sp_executesql #sql
END
To fix this i tried using things like quotename and played around with the format but nothing seems to have worked yet.
execute james_tester 'dbo.Calendar table' (That is the table I am wanting to query)
The problem lies in how you call your procedure.
execute james_tester 'dbo.Calendar table'
should be
execute james_tester 'dbo.Calendar'
Hence the error message :
Incorrect syntax near the keyword 'table'.
Table is a keyword of sql server.
So you couldn't use it as an alias with out [].
Try this
execute james_tester 'dbo.Calendar [table]'
or
execute james_tester 'dbo.Calendar t'
or
execute james_tester 'dbo.Calendar'

Incorrect syntax near the keyword “AS” when alter trigger

i created trigger called trgInsteadofdeleteEmp
and i just want to alter it, i wrote the following SQL code
alter trigger trgInsteadofdeleteEmp on Emp
instead of delete
as
begin
declare #id int , #name nvarchar(100)
select #id =id from deleted
select #name = name from deleted
insert into EmployeeAudit values (#id ,#name + 'tried to delete at' + GETDATE() as varchar(50))
end
and have the following output:
Msg 156, Level 15, State 1, Procedure trgInsteadofdeleteEmp, Line 8 Incorrect syntax near the keyword 'as'.
can someone point me in the direction of how to find the error
Thanks.
No, no, no, no, no.
Don't make the mistake of assuming that inserted and deleted have only one row. You are just putting errors in your code that are going to pop up at an unexpected time. I actually wish that SQL Server flagged this usage when creating the trigger.
Instead:
alter trigger trgInsteadofdeleteEmp on Emp
instead of delete
as
begin
insert into EmployeeAudit(id, name)
select id,
name + ' tried to delete at ' + convert(varchar(255), GETDATE(), 121) )
from deleted d;
end;
Your error is caused by the as. There seems to be a missing cast() function. But that is not the right fix. With date/times, use convert() or format() along with the desired format.
Other suggestions:
Always include the column names when doing an insert. In fact, an audit table really should have an identity id column, createdBy, and createdAt columns, all with default values.
Look at the strings that will be produced and be sure they are readable.
Use semicolons to end statements.
Don't rely on default formatting for date/time values.

Incorrect Syntax: Create Procedure must be the only statement in the batch

This question has been asked before but it all involved using "go" which I am not in need of here, at least I believe so.
I am following this tut https://www.youtube.com/watch?v=-xMGwiV5A6o, near the 1:25 mark exactly. And his seems to execute while mine doesn't.
Select * From Snacks
Create Proc spGetSnackByID
#Id int
as
Begin
Select Id, Name, Location
from Snacks where Id = #Id
End
Here is the exact error, being highlighted with the "BEGIN" statement:
"Msg 111, Level 15, State 1, Procedure spGetSnackByID, Line 7
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch."
If you want to keep the script as it is (select followed by a create procedure), you can construct the creation of the stored procedure in a NVARCHAR and EXECUTE it using sp_executesql. This way the CREATE statement is the first statement. Like this:
Select * From Snacks
EXECUTE sp_executesql N'
Create Proc spGetSnackByID
#Id int
as
Begin
Select Id, Name, Location
from Snacks where Id = #Id
End
';
Yes, SQL Server wants the create procedure as the first line. Just remark out the select above, it is not what you want anyway, because you have specified the fields in the stored procedure.

Is it possible to have dynamic sql based on a subquery without using variables?

I'd like to do the following in T-SQL:
EXEC('... ' + (SELECT ...))
A simple concrete example would be
EXEC('DROP TABLE ' + (SELECT name FROM sys.tables WHERE ...))
(Obviously, the WHERE clause is chosen such that the subquery always returns exactly one value.) When I try this, I get an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
I know that I can work around this by using variables, e.g.:
DECLARE #sql varchar(MAX)
SELECT #sql = 'DROP TABLE ' + name FROM sys.tables WHERE ...
EXEC(#sql)
but for various reasons I'd like to have only one statement. Is that possible?
PS: In case it's relevant, that's the dynamic SQL code I'm trying to squeeze into one statement:
How to drop SQL default constraint without knowing its name?
You can't do treatment on the value that you pass to the EXEC function.
Thus, you can't concatenate or otherwise transform the value, it as to be done prior to the call.
Thus the answer is, no you can't.
But like Pondlife said, if you give the reason why you want to do it this way, it will be possible to find a more satisfying answer.

Resources