I have a procedure that receives a table as a parameter. I need to fill it on then return it to another procedure that executes the first one. But, the problem is that when y compile I receive this error message:
Mens 352, Nivel 15, Estado 1, Procedimiento Person_InsertCloud, Línea 1
The table-valued parameter "#TableServerIds" must be declared with the READONLY option.
Can't a table as a parameter can be modified? What other options do I have?
Thanks
No the contents of the TVP can't be modified. Upvote Relax restriction that table parameters must be readonly when SPs call each other if you want this functionality.
The other option is a local #temp table. Stored procedures have read and write access to #temp tables created in parent scopes. But this does mean that callers of the sub procedure need to be aware of this requirement and create the expected #temp table before calling.
An example below
CREATE PROC P1
AS
CREATE TABLE #Foo
(
X VARCHAR(50)
);
EXEC P2;
SELECT *
FROM #Foo;
GO
CREATE PROC P2
AS
IF OBJECT_ID('tempdb..#Foo') IS NULL
BEGIN
RAISERROR ('This procedure expects table #Foo to already exist',16,1);
RETURN;
END
INSERT INTO #Foo
VALUES ('Inserted by P2')
GO
EXEC P1
Can't a Table as a parameter can be modified?
No. It is one of the restrictions on TVPs.
The restriction is documented:
Table-valued parameters must be passed as input READONLY parameters to Transact-SQL routines. You cannot perform DML operations such as UPDATE, DELETE, or INSERT on a table-valued parameter in the body of a routine.
Instead of filling a table, you can return a table from your second stored procedure using SELECT.
Related
I have a function that is defined as:
Where XXXXX is my schema YYYYY is my package.
PROCEDURE "XXXXX"."YYYYY.SPATIAL::SP_GA_PT_PATH_DISTANCE" (IN PID NVarChar(36))
LANGUAGE SQLScript
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
AS
BEGIN
I want to call a function and assign the result to a variable, I have tried the following two ways:
intIntersect := XXXXX.YYYYY.SPATIAL::GA_INTERSECT (32.925148, -117.020051,
32.924672, -117.019454,
32.924488, -117.020322,
32.924849, -117.019759);
SELECT XXXXX.YYYYY.SPATIAL::GA_INTERSECT (32.925148, -117.020051,
32.924672, -117.019454,
32.924488, -117.020322,
32.924849, -117.019759) INTO intIntersect FROM DUMMY;
I have played with different permutations of this, but nothing works.
Any ideas?
Thanks.
What you describe as a FUNCTION is really a PROCEDURE in your code example.
These differ in the ways you can call either of them.
Procedures need to be called via the CALL statement.
Functions can either be used as scalar function in all places where you can use expressions (i.e. the projection list of a SELECT-statement) or, for table-typed functions, like a table in the WHERE condition.
The parameters handed over to the procedure seem to be a list of data items.
The general way to pass "lists" of parameters is to use a table-type parameter:
CREATE FUNCTION "XXXXX"."YYYYY.SPATIAL::SP_GA_PT_PATH_DISTANCE"
(IN_PIDS TABLE (PID NVARCHAR(36)) )
RETURNS TABLE (DISTANCES DECIMAL)
AS
BEGIN
SELECT * FROM :IN_PIDS;
...
I am trying to create a stored procedure in MySQL which is not supposed to be vulnerable to SQL injection. Hence I am using prepared statements inside this. I have a Patient table to which I want to add data using this procedure. This is what my stored procedure looks like.
DROP PROCEDURE IF EXISTS CreatePatient;
DELIMITER ##
CREATE PROCEDURE CreatePatient (IN alias VARCHAR(20))
BEGIN
PREPARE q1 FROM 'insert into Patient values (?)';
set #alias = alias;
EXECUTE q1 USING #alias;
END ##
DELIMITER ;
When I tried to run this without setting a new variable #alias,
EXECUTE q1 USING alias;
I am getting an SQL syntax error. From my understanding, it doesn't seem right to create a variable within the method body just to assign it the input variable to the procedure. What am I missing here?
Mysql has 3 types of variables
User Defined Variables
Local variables
session variables
User defined variables have session scope while local variables have a block scope i.e within BEGIN-END Block.
Because local variables are in scope only during stored program execution, references to them are not permitted in prepared statements created within a stored program. Prepared statement scope is the current session, not the stored program, so the statement could be executed after the program ends, at which point the variables would no longer be in scope. For example, SELECT ... INTO local_var cannot be used as a prepared statement. This restriction also applies to stored procedure and function parameters
See official docs
Is it possible send to stored procedure arguments as rows from query?
I mean you have stored procedure
CREATE PROCEDURE XYZ (IN ARGUMENTS)
BEGIN
*/ how to get values from ARGUMENTS? /*
END;
and you send data to procedure in CALL directive like:
CALL XYZ(SELECT COL1,COL2 FROM TABLE WHERE CONDITION is true)
Or you have only to send CONDITION as a argument for procedure and invoke "SELECT" inside the store procedure?:(
Regards,
Artik
No, it's impossible in Mysql. You can only use Mysql Data Types as arguments of precedure, i.e. Mysql stored procedure/function can't use tables as arguments - only single-type values.
I have a simple custom type for use it as table valued parameter.
Like this:
CREATE TYPE dbo.PeriodsList AS TABLE
(
PeriodDate NVARCHAR(8) NOT NULL
)
I also have very simple table valued function:
alter FUNCTION GetPeriodsInYear (#periods dbo.PeriodsList READONLY) returns #PeriodsSet Table(period NVARCHAR(8))
BEGIN
insert #PeriodsSet
select
'0' as period
Return
end
But when I try to execute this function in this way
DECLARE #periods1 dbo.PeriodsList
INSERT INTO #periods1
VALUES ('20130916')
select * from GetPurchasesInYear(#periods1)
I receive error message - "Must declare the scalar variable "#periods1".
I have found many examples about stored procedures but not about functions. Is it possible to pass table valued parameters into functions?
And where the mistake in my code?
Check the "compatibility level" of your SQL Server 2008 database. If it's set to 80 (SQL Server 2000), you will get the error you are describing when you try to call a function (but not a stored procedure) that takes a table-valued parameter.
It should work if the compatibility level value is 90 (SQL Server 2005) or higher.
To view the compatibility level of your database, do:
SELECT compatibility_level
FROM sys.databases
WHERE name = 'YOUR_DB_NAME_HERE';
Source: http://msgroups.net/microsoft.public.sqlserver.programming/pass-table-valued-para/97879
Changing the compatibility level (SQL Server 2008): http://technet.microsoft.com/en-us/library/bb510680%28v=sql.100%29.aspx
[SQL Server 2008 Std]
I have a stored procedure that is used to insert new data into a table, which works as expected. However I now need to call this stored procedure multiple times using the results of a query as the parameters.
e.g.
select
name, age, foo, bar
from
sometable
where
wobble = 'true'
exec insertProc name age foo bar
I know I can use a cursor to acheive this, but I keep reading that "cursors are bad"...but I don't know any other way to do this?
One solution is to use cursor. Other is to prepare your result set into temp table before calling the procedure and then supply it to the procedure ( you have to alter the procedure by adding table-value param as input param). Some info in msdn.