create procedure dynamic in HANA XS - hana

I am trying to "translate" the following command into dynamic SQL to be used in a procedure:
SELECT *, 'IDS' AS SYSID FROM MODE_TEST.USR02
Can someone help with this question? I am facing problems with the 'IDS'.
thanks,
regards,

You can use like below:
do begin
declare str nvarchar(1000);
str='SELECT *,''IDS'' AS "SIYSID" FROM MODE_TEST.USR02';
execute immediate :str;
end;

Related

IBM DB2: Run SQL Scripts

Using the iSeries Navigator, the "Run SQL Scripts" utility.
Is it possible to use a global variable to do the following:
Create or Replace Variable FileName Char(21) Default 'V51BPCSF00.ESN'
Select * From FileName
Thanks
Theoretically, compound statements are supported (since 2014 in v7r1+ )so you might think to try something like so..
--create variable
Create or Replace Variable FileName Char(21) Default 'CWILT/MYTABLE';
then the compound statement
begin
declare myStmt varchar(500);
set myStmt = 'Select * from ' concat filename;
prepare s1 from myStmt;
execute s1;
end;
But that didn't work for me.
However, behind the scenes the DB is simply creating a stored procedure with your compound statement. You can simply do it yourself.
create or replace procedure cwilt5.test
language SQL
dynamic result sets 1
begin
declare myStmt varchar(500);
declare c1 cursor with return to client for s1 ;
set myStmt = 'Select * from ' concat filename;
prepare s1 from myStmt;
open c1;
end;
While the above uses the global variable as requested, I'd probably just pass in the file name as a parameter.
Key thing to realize is that variables aren't supported in the FROM clause of a static SQL statement. You have to use dynamic SQL, PREPARE & EXECUTE or OPEN if you need to return a result set.
Create or Replace Variable FileName Char(21) Default 'V51BPCSF00.ESN'
SELECT FileName from sysibm.sysdummy1
sysibm.sysdummy1 is a "fake" file to allow this type of select that needs a complete SQL statement.
I specify a library for the variable so I know where it is, and delete it afterwards if it's serving as a temporary variable
Create or Replace Variable MY_LIB.FileName Char(21) Default 'V51BPCSF00.ESN'
Do stuff .....
DROP Variable MY_LIB.FileName

sql server - fill results from executed query string in a temp table dynamically

I'm writing a stored procedure. I have a string which contains an sql query. For example:
DECLARE #sql nvarchar(max)
SET #sql = (N'SELECT pkOrderID FROM Orders')
(Just to note: this isn't what the select statement looks like. This is just an example of what I mean) I then want to execute the string and put the result in a temporary table E.g. #tempTable. I know EXEC(#sql) exists but not sure if it will do me any good in this situation. The other twist is that I do not know the names of all the columns in the returned #sql so the temp table #tempTable needs to be created dyanmically based off the return from #sql. Thanks for any help.
I think you could use SELECT INTO to do what you want but it would mean updating your string:
DECLARE #sql nvarchar(max)
SET #sql = (N'SELECT frompkOrderID INTO #tmporders FROM Orders')
then you should be able to run EXEC #sql to create the table
more information about SELECT INTO here : http://msdn.microsoft.com/en-au/library/ms188029.aspx
There is no simple way to do this. The problem with #JanR's solution is that the #tmporders table will be out of scope to the script that calls your stored procedure (ie It will produce an error like "Invalid object name '#rtmporders'"
One alternative is to use a global temp table (eg ##tmporders).
So your SP might look like this:
CREATE PROCEDURE TestSP
AS
BEGIN
SELECT pkOrderID INTO ##tmporders FROM Orders
END
GO
And the calling script might be like:
EXEC TestSP
SELECT * FROM ##temporders

store procedure in trans sql vs pl sql

In oracle (pl-sql) I have
s as (select * from ruleassigns$all where [snapshot] = p_Id);
inside a stored procedure. I want to port it into sql server (trans-sql), but when I write the same it gives me error on the structure of the statement. Please help!
I am wondering something like
declare #s = (select * from ruleassigns$all where [snapshot] = p_Id);
but that's not correct either.
Since this query is returning multiple values you'd probably want to use a Declare #table.
The syntax will be
Declare #s Table (p_id SomeDataType(probably int?))
You can use #s just like any other table.
SELECT * FROM #s
Note: I personally like the feel of temporary tables. They feel like normal tables.
You'd write:
CREATE TABLE #tableName (p_id SomeDataType)
and at the end of your SP you'd do DROP TABLE #tableName

Is it possible to execute a sql statement containing variable?

Is it possible to execute a sql statement containing variable?
DECLARE #SCHEMA varchar(40)
set #SCHEMA='X_SLF'
select * from #SCHEMA..Acc_tblCompany --// anyway to use this type of statement?
--below statement works
select * from X_SLF..Acc_tblCompany
--I don't want to do this following solution:
DECLARE #Sql nvarchar(80)
set #Sql=' select * from '+#SCHEMA+'..Acc_tblCompany'
Execute sp_executesql #Sql
Perhaps you could implement this as a case statement. It's not quite what you're asking for, but at least you can create the illusion to the caller that it's parameterized wrt schema

Parametrizing input using sql server?

I want to parametrize my stored procedure's input to prevent sql injection. The problem is MY database has no application(It's just for school) & as there's no client language like C# etc, I have to do it with sql itself. i did this
ALTER procedure [dbo].[drop_tt]
#ss varchar(40)
as
EXEC sp_executesql N'SELECT *
FROM tt
whERE ss = #Ss', N'#ss varchar(40)', #ss
but when I execute this statement the tt table was droped :(
exec drop_tt 'www';drop table tt--'
anyone can help?
In short: why are you altering sp? you just need to create a parametrized stored procedure like:
CREATE PROCEDURE uspGetAddress #City nvarchar(30)
AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City = #City
GO
Just look at this very simple tutorial , you don't need to alter your procedures.
Edit: my approach would be to get rid off the statement EXEC sp_executesql and naming that starts with drop. Just try to simplify your stored procedure execution statement in the body.