How to create a table variable for using in a JOIN? - hana

Because SAP HANA doesn't support CTEs, I'd like to learn to use table variables in joins.
I've found simple examples of using table variables in a join in SQL Server, like below:
https://www.youtube.com/watch?v=2fc6YUCQSV8
I have not found any simple examples of creating table variables in SAP HANA, let alone using them in a join.
Does anyone have a simple example of how to create a table variable and use it in a join in SAP HANA?
Thanks!

You can create table variables either by defining the structure and inserting values or you can create it by direct assignment. When using the variable name in a statement, you need to prefix with a colon. Please find some examples in the code below:
DO BEGIN
-- declaring table variables
DECLARE tab1 TABLE(ID INTEGER, PHRASE VARCHAR(20));
DECLARE tab2 TABLE(ID INTEGER, PHRASE VARCHAR(20));
-- filling values
INSERT INTO :tab1 VALUES (0, 'Hello');
INSERT INTO :tab1 VALUES (1, 'Hallo');
INSERT INTO :tab2 VALUES (0, 'World');
INSERT INTO :tab2 VALUES (1, 'Welt');
-- joining both
SELECT a.PHRASE, b.PHRASE
FROM :tab1 a
JOIN :tab2 b ON a.ID = b.ID;
-- declaring table variable via assignment
tab3 = SELECT 0 ID, '!' PHRASE FROM DUMMY;
-- joining all three
SELECT a.PHRASE, b.PHRASE, c.PHRASE
FROM :tab1 a, :tab2 b, :tab3 c
WHERE a.ID = b.ID;
END;
More detailed information with examples can be found in the documentation:
Table Variable Type Definition
Table Variables

Related

Use a returning value from an INSERT table (A) into another table (B)

I need to use the returning value of the Table A (ID) and insert it in Table B as a parameter:
insert into tableA (ID, Name , Address)
values (GEN_ID(GENERATOR,1),'John','123 street')
returning ID
--Example: ID=159
insert into tableB (ID, TABLE_A_FK )
values (GEN_ID(GENERATOR,1), 159)
Instead of entering the actual value 159, can I create like a variable (e.g. declare ID int;), and just pass the parameter?
The only way to do this in a single statement, is to use EXECUTE BLOCK (basically an anonymous one-off procedure). It uses the same syntax as normal stored procedures in Firebird.
You can do:
execute block
as
declare id integer;
begin
insert into tableA (ID, Name , Address)
values (GEN_ID(GENERATOR,1), 'John', '123 street')
returning ID
into id;
insert into tableB (ID, TABLE_A_FK)
values (GEN_ID(GENERATOR,1), :id);
end
If necessary, execute block statements can be parameterized, so you can use parameters to provide values (instead of hard coding them). See the link above for details.

Using OUTPUT INTO with from_table_name in an INSERT statement [duplicate]

This question already has answers here:
Is it possible to for SQL Output clause to return a column not being inserted?
(2 answers)
Closed 2 years ago.
Microsoft's OUTPUT Clause documentation says that you are allowed to use from_table_name in the OUTPUT clause's column name.
There are two examples of this:
Using OUTPUT INTO with from_table_name in an UPDATE statement
Using OUTPUT INTO with from_table_name in a DELETE statement
Is it possible to also use it in an INSERT statement?
INSERT INTO T ( [Name] )
OUTPUT S.Code, inserted.Id INTO #TMP -- The multi-part identifier "S.Code" could not be bound.
SELECT [Name] FROM S;
Failing example using table variables
-- A table to insert into.
DECLARE #Item TABLE (
[Id] [int] IDENTITY(1,1),
[Name] varchar(100)
);
-- A table variable to store inserted Ids and related Codes
DECLARE #T TABLE (
Code varchar(10),
ItemId int
);
-- Insert some new items
WITH S ([Name], Code) AS (
SELECT 'First', 'foo'
UNION ALL SELECT 'Second', 'bar'
-- Etc.
)
INSERT INTO #Item ( [Name] )
OUTPUT S.Code, inserted.Id INTO #T -- The multi-part identifier "S.Code" could not be bound.
SELECT [Name] FROM S;
No, because an INSERT doesn't have a FROM; it has a set of values that are prepared either by the VALUES keyword, or from a query (and even though that query has a FROM, you should conceive that it's already been run and turned into a block of values by the time the insert is done; there is no s.code any more)
If you want to output something from the table that drove the insert you'll need to use a merge statement that never matches any records (so it's only inserting) instead, or perhaps insert all your data into #tmp and then insert from #tmp into the real table - #tmp will thus still be the record of rows that were inserted, it's just that it was created to drive the insert rather than as a consequence of it (caveats that it wouldn't contain calculated columns)

How do i INSERT/UPDATE a table varieble from a SELECT query from another table in the database?

I am trying to insert/update a select query results to a table variable in sql-server stored procedure but for some reason only the first value is being updated
declare #student table(regdno int,semester_marks float null,temp float null);
insert into #student(regdno )
select regdno
from sem_marks
where allocId =#allocid
order by regdno asc;
AND
update #student
set regdno = sem_marks.regdno
from sem_marks
where sem_marks .allocId =#allocid ;
declare #student table(regdno int,semester_marks float ,temp float );
declared the table variable
need to insert the values of regdno from another table sem_marks into #student leaving columns semester_marks and temp blank currently which will be updated later on the the code
You just need to add the fields. Start with reading the documentation if you get stuck...
Here's a perfect example for your insert:
https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql?view=sql-server-2017#l-inserting-data-into-a-table-variable
https://learn.microsoft.com/en-us/sql/t-sql/queries/update-transact-sql?view=sql-server-2017

How do I create a variable/parameter that is a string of values in SQL SSMS that I can use as a substitute in my where clause?

This may be a very basic question, but I have been struggling with this.
I have a SSMS query that I'll be using multiple times for a large set of client Ids. Its quite cumbersome to have to amend the parameters in all the where clauses every time I want to run it.
For simplicity, I want to convert a query like the one below:
SELECT
ID,
Description
From TestDb
Where ID in ('1-234908','1-345678','1-12345')
to a query of the format below so that I only need to change my variable field once and it can be applied across my query:
USE TestDb
DECLARE #ixns NVARCHAR(100)
SET #ixns = '''1-234908'',''1-345678'',''1-12345'''
SELECT
ID,
Description
From TestDb
Where ID IN #ixns
However, the above format doesn't work. Can anyone help me on how I can use a varchar/string variable in my "where" clause for my query so that I can query multiple IDs at the same time and only have to adjust/set my variable once?
Thanks in advance :D
The most appropriate solution would be to use a table variable:
DECLARE #ixns TABLE (id NVARCHAR(100));
INSERT INTO #ixns(id) VALUES
('1-234908'),
('1-345678'),
('1-12345');
SELECT ID, Description
FROM TestDb
WHERE ID IN (SELECT id FROM #ixns);
You can load ids to temp table use that in where condition
USE TestDb
DECLARE #tmpIDs TABLE
(
id VARCHAR(50)
)
insert into #tmpIDs values ('1-234908')
insert into #tmpIDs values ('1-345678')
insert into #tmpIDs values ('1-12345')
SELECT
ID,
Description
From TestDb
Where ID IN (select id from #tmpIDs)
The most appropriate way is to create a table type because it is possible to pass this type as parameters.
1) Creating the table type with the ID column.
create type MyListID as table
(
Id int not null
)
go
2) Creating the procedure that receives this type as a parameter.
create procedure MyProcedure
(
#MyListID as MyListID readonly
)
as
select
column1,
column2
...
from
MyTable
where
Id in (select Id from #MyListID)
3) In this example you can see how to fill this type through your application ..: https://stackoverflow.com/a/25871046/8286724

sql query to create a table at runtime and insert the values in it from the select statement from the database

what i am tryin to do is make a table(#tbl) runtime and insert the data from the select statement from the database,as what i have done so far is
declare #tbl TABLE (
Item int
)
begin
insert into #tbl values select cid from tbl_custumer where cus_ph like '%'+'987'+'%'
select * from #tbl
end
as "select cid" statement returns multiple records
I think you might want the code to look like this:
begin
declare #tbl TABLE (
Item int
);
insert into #tbl(Item)
select cid
from tbl_custumer
where cus_ph like '%'+'987'+'%';
select *
from #tbl;
end;
Notes:
The begin/end block is not really necessary, but I'm guessing you want it for other reasons (a stored procedure, if, or something similar).
The values keyword is not needed when using insert . . . select.
Use semicolons at the end of each SQL statement. Although they are optional, they make the code easier to follow.