Hi I am trying hard to write a stored procedure in ISeries DB2 but having errors.
create procedure pakretst.fttest2
(IN fExpression CHARACTER(10))
language sql
reads sql data
dynamic result sets 1
begin
declare stmt VARCHAR(50);
declare x cursor for sl;
If ftExpression IS NOT NULL
set stmt='select * from pakretst.uwftrtystp WHERE'+ftExpression;
else
set stmt='select * from pakretst.uwftrtystp';
prepare sl from stmt;
open x;
return;
end
;
****SQL State: 42618
Vendor Code: -312
Message: [SQL0312] Variable FTEXPRESSION not defined or not usable. Cause . . . . . : The variable FTEXPRESSION appears in the SQL statement, but one of the following conditions exists: -- No declaration for the variable exists. --**
I don't know if this is a simple error in transcription, but in what you given here, you have
(IN fExpression CHARACTER(10))
in the parameter list but
set stmt='select * from pakretst.uwftrtystp WHERE'+ftExpression;
at the point of usage - fExpression versus ftExpression...
Related
CREATE OR REPLACE PROCEDURE dbo.test()
RETURNS varchar null
LANGUAGE JAVASCRIPT
execute as caller
AS
$$
set MY_VAR =1;
return $MY_VAR;
$$
;
If I execute as below I am facing the error . Message as shown below
call dbo.test();
//ERROR: JavaScript compilation error: Uncaught SyntaxError: Unexpected identifier in TEST at 'set MY_VAR =1;' position 4
CREATE OR REPLACE PROCEDURE test()
RETURNS float not null
LANGUAGE JAVASCRIPT
execute as caller
AS
$$
var MY_VAR =1.1;
return MY_VAR;
$$
;
call <database>.<schema>.test();
gives:
1.1
so given you are trying to return a number value, I changed it to float instead of varchar, but after testing ether is fine.
But mostly I changed from set to var to declare your variable. The other valid keyword is let, which also works.
Im want to use a temporary table with dynamic sql but i have an error saying that the column Column, parameter, or variable #3: Cannot find data type MyTable. Must declare the table variable "#CountriesFound".
Here's my code
ALTER PROCEDURE sqlDynamic #countryId int
AS
CREATE TYPE MyTable AS TABLE ([countries] varchar(50));
DECLARE #statement NVARCHAR(MAX)
DECLARE #CountriesFound AS MyTable
INSERT INTO #CountriesFound(countries)
select Name
FROM CountriesFound
SET #statement =
'SELECT name, CASE WHEN (c.name IN (SELECT countries FROM #CountriesFound)) THEN 1
ELSE 0
END as countryFound FROM Country c WHERE c.countryId = #countryId'
EXECUTE sp_executesql #statement,
N'#countryId int, #CountriesFound MyTable READONLY',
#countryId = #countryId,
#CountriesFound=#CountriesFound;
Any idea how to fix it ?
The error you are getting has nothing to do with dynamic sql. You cannot create a stored procedure that uses a User-Defined DataType (MyTable in your example) in the same procedure that is going to create the TYPE. The TYPE has to already be created before the procedure will even let the code compile.
Consider this simplified code:
CREATE PROCEDURE TypeTest
AS
CREATE TYPE MyTable AS TABLE ([Id] int);
DECLARE #mt AS MyTable;
Just executing this will give the error:
Msg 2715, Level 16, State 3, Procedure TypeTest, Line 1 Column,
parameter, or variable #1: Cannot find data type MyTable. Parameter or
variable '#mt' has an invalid data type.
You need to CREATE the TYPE outside of the procedure code before you try to create the procedure. It doesn't make sense to CREATE a TYPE inside a procedure code, since TYPE objects are permanent on the server until they are dropped anyway, and procedures are usually created to be run multiple times.
It would be nice if the SQL Server gave an error message more to this effect, but SQL Server isn't famous for having the most helpful error messages.
Using Historian 5.5 and SQL Server 2012.
I have a stored procedure in SQL Server called perfEng_RWtopits and I want to call this procedure from within a calculation tag in Historian Administrator.
The stored procedure returns one float value.
I have the following code so far:
Dim sql
Dim con
Dim cmd
Dim value
sql = "perfEng_RWtopits"
Set con = CreateObject("ADODB.Connection")
con.ConnectionString = "myconnectionstring"
con.Open
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandText = sql
value = cmd.Execute
con.close
Set cmd = Nothing
Set con = Nothing
When I test the calculation I get a value of zero and a quality of bad. If I execute the stored procedure within SQL Server I get 17.123554 (which is correct). Also if I add the following to the end.
Result = value
I get the following error message.
Can anyone help?
You have an error in your vb script. To bad that we can't debug that in Historian Administrator in calcullation tab.
When using a parameterless function you can execute it by writing:
FunctionName
or
FunctionName(), but when you want to pass the result to a variable then you must use () like this value = FunctionName().
You are using function cmd.Execute without () and returning the result to value. Just fix the line
value = cmd.Execute
to
value = cmd.Execute()
I am getting an error when I try to execute the script that is given at end of this post. My requirement is to check for procedure's existence, then drop it if it exists and finally create the procedure.
How would I do this using a single script file?
Procedure Proc_GenerateTestData. 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch. SQL2.sql 12 1
The SQL script that always throws above error is as below.
IF OBJECT_ID('dbo.Proc_GenerateTestData', 'p') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.Proc_GenerateTestData
END
CREATE PROCEDURE dbo.Proc_GenerateTestData #numberOfRecords INT = 100
AS
--drop table #temp1
DECLARE #currentTime AS TIME = CAST(GETDATE() AS TIME);
DECLARE #currentWorkShiftDate AS DATETIME = CAST(CAST(GETDATE() AS DATE) AS DATETIME);
DECLARE #startTime TIME,
#lastShiftStartTime TIME;
DECLARE #tenantId AS UNIQUEIDENTIFIER = (SELECT TOP (1)
Tenants.Id
FROM Tenants
ORDER BY Tenants.Id ASC);
DECLARE #workshiftId INT,
#lastShiftStartDate DATETIME;
--more statements for this procedure follow
As the error clearly says - the CREATE PROCEDURE must be the first statement in a query batch.
This is not the case here, since you have the check for the DROP before hand.
You need to change your script so that the CREATE PROCEDURE statement is really the first in the query batch. If you're running this in SQL Server Management Studio, just add a GO delimiter before the CREATE.
How do I execute a SP and get the return value. The below code always returns null object. The storedprocedure has been tested in the database using the same parameters as in code, but the SubSonic sp always returns null. When executed in the db via sql, it returns the correct values.
This is using SubSonic 3.0.0.3.
myDB db = new myDB();
StoredProcedure sp = db.GetReturnValue(myParameterValue);
sp.Execute();
int? myReturnValue = (int?)sp.Output;
In the above code, sp.Output is always null. When executed in the database, the returned variable is a valid integer (0 or higher) and is never null.
Stored procedure code below:
CREATE PROCEDURE [dbo].[GetReturnValue]
#myVariable varchar(50)
AS
declare #myReturn int
BEGIN
set #myReturn = 5;
return #myReturn;
END
When executing the stored proc in SQL Server, the returned value is '5'.
I copied your sproc and stepped through the SubSonic code and .Output is never set anywhere. A work around would be using an output parameter and referring to it after executing: sproc.OutputValues[0];
Here's a simple way to do it:
In the stored procedure, instead of using RETURN, use SELECT like this:
SELECT ##ROWCOUNT
or
SELECT #TheIntegerIWantToReturn
Then in the code use:
StoredProcName.ExecuteScalar()
This will return the single integer you SELECTED in your stored procedure.
CREATE PROCEDURE [dbo].[GetReturnValue]
#myVariable varchar(50)
#myReturn BIGINT OUTPUT
AS
declare #myReturn int
BEGIN
set #myReturn = 5;
return #myReturn;
END