I am writing a SQL stored procedure that will be called in Access. My sp will be passed four data fields(BatchID, InstrumentName, FileName,QueueId). Then it will insert a record into a table (tblinstrumentInterfaceLog). Here is my code thus far:
CREATE PROCEDURE upInsertToInstrumentInterfaceLog #BatchID int, #InstrumentName nvarchar(60), #FileName nvarchar(60), #QueueID int
INSERT INTO tblInstrumentinterfaceLog (batchId,Instrumentname,"Filename",QueueID,DateOfBatch,folder)
VALUES (#batchid,#InstrumentName,#FileName,#QueueID,#getdate(),'New')
GO
I believe I have the correct format but I get two errors:
Msg 156, Level 15, State 1, Procedure upInsertToInstrumentInterfaceLog, Line 2
Incorrect syntax near the keyword 'INSERT'.
Msg 137, Level 15, State 2, Procedure upInsertToInstrumentInterfaceLog, Line 3
Must declare the scalar variable "#QueueID".
To Make sure there wasn't perhaps a datatype issue I looked at the schema of tblInstrumentInterfaceLog and this it seemed to match the way I initialized each.
Could someone tell me if they see an issues with this stored procedure
You need an AS after your parameter declarations and your #getdate() command is just getdate()
CREATE PROCEDURE upInsertToInstrumentInterfaceLog
#BatchID int, #InstrumentName nvarchar(60), #FileName nvarchar(60), #QueueID int
AS
BEGIN
INSERT INTO tblInstrumentinterfaceLog (batchId,Instrumentname,"Filename",QueueID,DateOfBatch,folder)
VALUES (#batchid,#InstrumentName,#FileName,#QueueID,
getdate(),'New')
END
GO
You forgot the AS
CREATE PROCEDURE upInsertToInstrumentInterfaceLog #BatchID int, #InstrumentName nvarchar(60),
#FileName nvarchar(60), #QueueID int
AS
INSERT INTO tblInstrumentinterfaceLog (batchId,Instrumentname,"Filename",QueueID,DateOfBatch,folder)
VALUES (#batchid,#InstrumentName,#FileName,#QueueID,#getdate(),'New')
GO
Related
I'm trying to use tSQLt to test a stored procedure that returns JSON data. The database is running under SQL Server 2016. The stored procedure is as follows (simplified considerably):
CREATE PROCEDURE [SearchForThings]
#SearchText NVARCHAR(1000),
#MaximumRowsToReturn INT
AS
BEGIN
SELECT TOP(#MaximumRowsToReturn)
[Id],
[ItemName]
FROM
[BigTableOfThings] AS bt
WHERE
bt.[Tags] LIKE N'%' + #SearchText + N'%'
ORDER BY
bt.[ItemName]
FOR JSON AUTO, ROOT(N'Things');
END;
This can't be tested in the same way XML can - I've tried a test table, as below, which was suggested in this related answer here -
CREATE TABLE #JsonResult (JsonData NVARCHAR(MAX))
INSERT #JsonResult (JsonData)
EXEC [SearchForThings] 'cats',10
The above code produces this error:
The FOR JSON clause is not allowed in a INSERT statement.
I cannot alter the stored procedure under test. How can I capture the JSON result?
Without being able to modify the stored proc, your last ditch effort would be to use OPENROWSET. Here's how you would call it in your case:
INSERT INTO #JsonResult
SELECT *
FROM OPENROWSET('SQLNCLI', 'Server=[ServerNameGoesHere];Trusted_Connection=yes;','EXEC SearchForThings ''cats'',10')
If you get an error, you can use the following to enable ad hoc distributed queries:
sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
I know this is two years on but I stumbled on this today when trying to solve a different tSQLt problem.
Your issue occurs because the column being returned from your stored procedure is not explicity named. If you provide a column name for the JSON data you can insert the data into a #temp table, e.g.:
create table BigTableOfThings (
Id int not null,
ItemName nvarchar(50) not null,
Tags nvarchar(50) not null
);
insert BigTableOfThings values
(1, 'Whiskers', 'Cool for Cats'),
(2, 'Barkley', 'Dogs Rule!');
GO
create procedure SearchForThings
#SearchText nvarchar(1000),
#MaximumRowsToReturn int
as
begin
select [JsonData] = (
select top(#MaximumRowsToReturn)
Id,
ItemName
from
BigTableOfThings as bt
where
bt.Tags like N'%' + #SearchText + N'%'
order by
bt.ItemName
for json auto, root(N'Things')
);
end
go
create table #JsonResult (JsonData nvarchar(max));
insert #JsonResult (JsonData)
exec SearchForThings 'cats',10;
select * from #JsonResult;
go
Which yields...
{"Things":[{"Id":1,"ItemName":"Whiskers"}]}
I am trying to write a simple procedure but am encountering a syntax error at the first parameter. As best I can tell I'm following the syntax of CREATE PROCEDURE correctly.
I am limited to accessing my database with phpMyAdmin. Here is the create script I'm trying to run:
DROP PROCEDURE IF EXISTS product_index_swap/
CREATE PROCEDURE product_index_swap (#id INT, #oldIndex INT, #newIndex INT)
BEGIN
DECLARE #swapID;
SET #swapID = (SELECT `id` FROM `product` WHERE `order_index` = #newIndex LIMIT 1);
UPDATE `products` SET `order_index` = (CASE WHEN `id` = #id THEN #newIndex
WHEN `id` = #swapID THEN #oldIndex END)
WHERE `id` IN (#id, #swapID);
END
I am using the option on phpMyAdmin to change the delimiter to /.
I receive a syntax error "near '#id INT, #oldIndex INT....". I thought I may encounter more delimiter errors since I'm not entirely clear on the scope of them. I believe if that was the problem the error would be on a new line in the procedure when it failed to understand a semicolon, not at the parameters declaration.
You're using the Microsoft SQL Server convention of putting # before all the parameters and local variables. MySQL doesn't do this.
In MySQL syntax, procedure parameters have no sigil.
Also parameters are typically declared IN or OUT or INOUT.
CREATE PROCEDURE product_index_swap (IN id INT, IN oldIndex INT, IN newIndex INT)
BEGIN
DECLARE swapID;
...
MySQL variables that have the # sigil are session variables.
See also:
https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html
https://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html
https://dev.mysql.com/doc/refman/5.7/en/set-variable.html
In MySQL, the #var variables are session level variables.
Use normal variables without the # and make sure you do not have conflict with column names:
CREATE PROCEDURE product_index_swap (in_id INT, in_oldIndex INT, in_newIndex INT)
BEGIN
DECLARE v_swapID int;
SELECT id into v_swapID
FROM product
WHERE order_index = in_newIndex
LIMIT 1;
UPDATE products
SET order_index = CASE WHEN id = in_id THEN in_newIndex
WHEN id = v_swapID THEN in_oldIndex
END
WHERE id IN (in_id, v_swapID);
END
After doing some research here and online I am at a loss as to whether this is possible. What I want to do is call a stored procedure that has several parameters one of which is a table-value parameter.
This is my stored procedure snippet:
ALTER PROCEDURE [dbo].[procName]
#Action nvarchar(10) = 'view'
,#var1 int = 0
,#var2 int = 0
,#var3 myType ReadOnly
I now have another procedure (proc2) that has the following lines:
insert into #varX
select top 5
field1, field2
from
sourceTable
print 'Processing from table values...'
exec dbo.procName 'refresh', -1, 0, #varX
Note that varX and var3 are of the same type MyType
When I execute proc2 I get the error that I am specifying too many arguments for dbo.procName
I am at the point in thinking it is not possible to specify multiple parameters inclusive of a table-value parameter to a stored procedure. I am now tending towards the thought of changing my procName definition to only have one parameter (as all of the examples online seem to have) and have my table-value paramter act as an array of parameter values inclusive of the information I had in my previous select statement (in proc2). If however it is possible to do this call, please illustrate how this is done.
Thanks
This compiles and runs for me:
create type TT as table (ID int not null);
go
create procedure P1
#Val1 int,
#Val2 TT readonly,
#Val3 int
as
select #Val1 as Val1,ID,#Val3
from #Val2;
go
create procedure P2
as
declare #T TT;
insert into #T(ID) values (1),(2)
exec P1 10,#T,13
go
exec P2
Result:
Val1 ID
----------- ----------- -----------
10 1 13
10 2 13
So, I don't know what your issue is, but it's not being able to mix table and non-table parameters.
Does anybody know if this is allowed?
IF CALL GET_RIGHT_NODE(edge) = 15
THEN
SELECT "IT WORKS";
I'm getting an error on this syntax, is it possible any other way?
The return values from stored procedures should be captured in OUT paramters (whereas those from user defined functions can be captured as #returnValue = function()).
So, your GET_RIGHT_NODE should take an OUT parameter and set it to the return value.
CREATE PROCEDURE GET_RIGHT_NODE
(
#edge INT,
#returnValue INT OUTPUT
)
AS
-- Definition of the proc.
then you would call the procedure as follows:
DECLARE #returnValue INT
CALL GET_RIGHT_NODE(#edge, #returnValue)
IF (#returnValue = 15)
THEN
SELECT 'IT WORKS'
Multivalue insert example - it works manually but NOT in mySQL stored procedure.
INSERT INTO input_data1(mobile) VALUES (9619825525),(9619825255),(9324198256),(9013000002),(9999999450),(9999999876) ;
i am getting syntax error near "str" word in below proc, Can any one let me know how to implement this multi value INSERT work in procedure?
DELIMITER |
DROP PROCEDURE IF EXISTS mobile_series1;
CREATE PROCEDURE mobile_series1(IN str text)
LANGUAGE SQL READS SQL DATA
BEGIN
DROP TABLE IF EXISTS input_data1 ;
CREATE TEMPORARY TABLE input_data1 (mobile varchar(1000)) engine=memory;
INSERT INTO input_data1(mobile) VALUES str;
END |
DELIMITER ;
Thanks in Advance.
I don't have a MySQL server so there's probably syntax errors and +1 errors (i.e. may not be capturing the last on the list, may not progress past the first item etc, problems fixed by putting a +1 in the code), but you basically want to replace your INSERT statement with something this.
DECLARE INT _CURSOR 0;
DECLARE INT _TOKENLENGTH 0;
DECLARE VARCHAR _TOKEN NULL;
SELECT LOCATE(str, ",", _CURSOR) - _CURSOR INTO _TOKENLENGTH;
LOOP
IF _TOKENLENGTH <= 0 THEN
SELECT RIGHT(str, _CURSOR) INTO _TOKEN;
INSERT INTO input_data1(mobile) VALUE _TOKEN;
LEAVE;
END IF;
SELECT SUBSTRING(str, _CURSOR, _TOKENLENGTH) INTO _TOKEN;
INSERT INTO input_data1(mobile) VALUE _TOKEN;
SELECT _CURSOR + _TOKENLENGTH + 1 INTO _CURSOR;
SELECT LOCATE(str, ",", _CURSOR + 1) - _CURSOR INTO _TOKENLENGTH;
END LOOP;
Your function call would then be something like
EXEC mobile_series1('9619825525,9619825255,9324198256')