Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.36/5 (3 votes)
See more:
Getting Some error
Here my mysql Code i want to write in sql server

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_CallStatement`
(
IN vStmt VARCHAR(1000)
)
BEGIN
SET @sql=vStmt;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END$$

DELIMITER ;


//error/
Msg 137, Level 15, State 1, Procedure sp_CallStatement, Line 8
Must declare the scalar variable "@sql".
Msg 102, Level 15, State 1, Procedure sp_CallStatement, Line 11
Incorrect syntax near 'PREPARE'.

What I have tried:

CREATE PROCEDURE [dbo].[sp_CallStatement]
@Stmt VARCHAR(1000)
as
BEGIN
SET @sql=vStmt;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END
Posted
Updated 1-Apr-19 1:27am
v2
Comments
RedDk 16-Feb-18 12:55pm    
Using MSSQL, somewhere in the lines above, there would be a DECLARE @sql. As for "mysql" ... what's that?

Msg 137, Level 15, State 1, Procedure sp_CallStatement, Line 8
Must declare the scalar variable "@sql".
As the error message says: you don't declare @sql so it doesn't know what to do with it.

Msg 102, Level 15, State 1, Procedure sp_CallStatement, Line 11
Incorrect syntax near 'PREPARE'.
PREPARE is not an SQL keyword: it doesn't know what you mean: Reserved Keywords (Transact-SQL) | Microsoft Docs[^]
Prepare is a PHP construct, not SQL. Which leaves your SP wide open to SQL injection all over again...
 
Share this answer
 
--This one is working
CREATE PROCEDURE [dbo].[sp_CallStatement]
@Stmt nvarchar(max)
as
BEGIN
SET NOCOUNT ON;
Declare @sql nvarchar(max)
SET @sql=@Stmt
EXECUTE @Stmt;
END
GO
 
Share this answer
 
v3
Comments
Nirav Prabtani 1-Apr-19 7:32am    
+5
CHill60 1-Apr-19 11:12am    
What is the point of having @sql at all in this SP? It's never used
Member 14207397 1-Apr-19 15:41pm    
-- Syntax for SQL Server(so took @sql)
-- Declare the variable to be used.
DECLARE @MyCounter int;

-- Initialize the variable.
SET @MyCounter = 0;--(Set their values)
local variable named @sql to retrive value
Richard Deeming 4-Apr-19 13:38pm    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900