EXECUTE AS on truncate sproc - Incorrect syntax - sql-server-2008

I'm getting an Incorrect syntax when trying to create a stored procedure to truncate a table then reseed it. Here's my code
CREATE PROCEDURE [dbo].[_TransportZipporah_Purge]
WITH EXECUTE AS owner
AS
TRUNCATE TABLE [dbo].[*tablename*];
GO
DBCC CHECKIDENT ('dbo.*tablename*', RESEED, 0);
As i have got this direct from MSDN I would have thought it would be correct.
Database server i'm using is : SQL Server 2008 v10.0.4064.0
Can anyone help please?
Thanks in advance
Scott

The syntax is correct - but you cannot have a GO statement inside a proc.

Related

Run stored procedure included sql script using robot framework

I want to run sql script which include db and table creations and stored procedure creations.
but when I try to run sql script using execute sql script keyword in database library I get an error like below
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER $$\n CREATE OR
REPLACE PROCEDURE `proc_GetCustomerDetails`(\n I...' at line 2")
before procedure I have delimiter like this,
DELIMITER $$
CREATE OR REPLACE PROCEDURE `proc_GetCustomerDetails`(
IN CustomerNbr LONGTEXT,
IN Lang VARCHAR(5)
)
DETERMINISTIC
BEGIN
IF Lang IS NULL THEN SET lang = "fin";
END IF;
SELECT * from dbname.customer;
END;$$
DELIMITER ;
If I comment stored procedure part, sql file is running without errors with rest of the table creation statements.
I googled this and couldn't find any related issue. I saw we have call stored procedure keyword. but I want to keep table creations and stored procedures in same sql file and need to run. I use MariaDB for this task.
Libraries used,
pymysql
robotframework-databaselibrary
If I run sql file using HeidiSQL it is running without any errors with procedures and delimiters. That mean there are no sql errors.
Can Someone tell me how to fix this?
DELIMITER is a statement supported only for the client, it is not supported by the server; thus the error. The solution - drop it.
Here's a question with very good answers what is it and why it's needed.
In short - when you work with a client you need a way to instruct it "this is not a statement you should execute immediately, this is still just a line in the SP you'll be sending to the server" - so you tell (to the client) "the DELIMITER b/n statements is temporarily $$". The server doesn't need/care about that - it knows everything between CREATE PROCEDURE, BEGIN, END are connected statements, a block.
When you connect to the DB through API (pymysql) vs an interactive client (shell, heidisql, etc) - you're sending the SP as a block, there's no way its statements will be ran one by one, thus the DELIMITER is not needed, not a supported command by the server, and generates an error. Drop it.

How to declare If Statement in mysql triggers

I'm trying to create a trigger in phpmyadmin. Can anyone tell me what's wrong with this query, it keeps telling me MySQL error #1064, SQL Syntax
You have no BEGIN at the start of the trigger body, to match the end on the last line.

How can we create procedure in BigSQL?

I am trying to create procedure in BigSQL. where i got some syntax error since i used db2 systax.Could you please tell me. what is the systax for procedure creation in BigSQL.
The documentation for the BigSQL syntax can be found here: http://www-01.ibm.com/support/knowledgecenter/SSPT3X_4.0.0/com.ibm.swg.im.infosphere.biginsights.db2biga.doc/doc/biga_intro.html?lang=en
However, as noted, you probably are dealing with a specific syntax issue and you should post the code for the procedure you created.

MS Access - Syntax error on CREATE TABLE Statement in MySql - using Trigger

For some reason, i can't get this to work..
CREATE TRIGGER triggerupdate
INSTEAD OF UPDATE ON ORDERDETAILS
AS
IF UPDATE(ORDERVALUE) BEGIN
Print ('INSTEAD OF Trigger [triggerupdate] - Trigger executed!!')
Print('You cannot update Order Value')
END
I'm trying to do a trigger if someone tries to update OrderValue. IT kept saying that Syntax error on Create Table.
Let me know what I have wrong.
Thanks
You can't use MS Access. Access does not support TRIGGERs, that's why you're getting Syntax errors, because "CREATE TRIGGER" is not supported SQL in Access.
Also, Print isn't a function in Access SQL either.
It's possible that you're meant to use Access as a front-end for MSSQL Server, which can be done, and where your SQL statements will become valid (but your use of Print is archaic).

How to insert the SQL Error message into table

I want to store the error messages which occurs while executing the stored procedure in another error table.
Here is the my sample procedure having some error statements.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`$$
CREATE PROCEDURE `test`()
BEGIN
SELECT * FROM emp;
END $$
DELIMITER $$;
When i call the above procedure it gives me error.I wnat to store this error code & message in another table as "error".
Any pointers are appreciated.
Thanks in Advance.
The standard way to handle this in MySQL is to declare a HANDLER to handle the error condition the way you want. This allows you to insert an error message into another table if you want, and then to either CONTINUE or EXIT the running procedure as required.
Here's the documentation:
http://dev.mysql.com/doc/refman/5.0/en/declare-handler.html
Unfortunately you will not be able to access the SQLSTATE of the statement that caused the error, so this approach is somewhat limited.
Here's another relevant question on SO with much more detail:
MySQL Stored Procedure Error Handling
SQL ERROR means there is something wrong with your SQL Query. It may (or may not) depend on your table. If the error is TABLE SPECIFIC or QUERY SYNTAX ERROR, that is, if there is something wrong with a specific table ONLY, or with a query, then definitely you can insert your errors into a table. But, if there is something with your CONNECTION or something else, then you cannot insert errors into any table.
ALWAYS try to log your errors in a html file or txt (text) file, so that you can smoothly access it. Also there is a less chance for failure.