HANA SQL Select Count (*) from multiple tables found in a table - mysql

DECLARE VI_CNT INTEGER DEFAULT 0;
DECLARE VI_IDX INTEGER;
DECLARE VI_LIMIT INTEGER;
DECLARE VS_OUTPUTSTRG1 NVARCHAR(500);
DECLARE VS_OUTPUTSTRG2 NVARCHAR(500);
/* ANAGRAFICA TABELLE FLUSSI */
ANAGRAFICA = SELECT *
FROM (SELECT DISTINCT
ZCSOURSYS,
ZTABLE,
ROW_NUMBER() OVER (ORDER BY ZCSOURSYS) AS ROW_NB
FROM ZDAFNE_INFO);
/************ FOR ***********/
SELECT COUNT (ZTABLE) INTO VI_LIMIT FROM :ANAGRAFICA;
FOR VI_IDX IN 1..:VI_LIMIT DO
VI_CNT = :VI_IDX;
SELECT ZTABLE INTO VS_OUTPUTSTRG1 FROM :ANAGRAFICA WHERE ROW_NB = VI_IDX;
END FOR;
VS_OUTPUTSTRG2 := 'INSERT INTO "TEAMBW"."IFRS17.INTEGRATION.DATA_QUALITY::ZTB_DQ_DAFNE_TEST" SELECT COUNT(*) FROM '||:VS_OUTPUTSTRG1||'';
EXECUTE IMMEDIATE (:VS_OUTPUTSTRG2);
Hello everyone! Thanks in advance!
Any help about this? The output doesn't insert anything... maybe I'm doing something wrong?

It looks like the OP wants to store the raw record count of a list of tables into yet another table.
This requirement can be met without the use of SQLScript.
SAP HANA keeps the number of committed records in tables available in catalog tables like [M_TABLES][1].
With this information available, the INSERT-statement can be rewritten like so:
INSERT INTO
"TEAMBW"."IFRS17.INTEGRATION.DATA_QUALITY::ZTB_DQ_DAFNE_TEST"
(TABLE_NAME, RECORD_COUNT)
(SELECT
TABLE_NAME, RECORD_COUNT
FROM M_TABLES
WHERE
SCHEMA_NAME ='xyz'
AND TABLE_NAME IN (SELECT DISTINCT TABLE_NAME
FROM ZDAFNE_INFO)
);
This solution works as long as no filtering of to-be-counted records in the source tables is required.

Related

SQL method for returning data from multiple tables based on column names

I am trying to do something a little weird and cannot figure out the right method for getting it done. Essentially I am trying to pull all tables/views and columns where the column name is like some string. In addition to that I would like to pull 1 row of data from that table/view and column combination. The second part is where I am lost. I know I can pull the necessary tables/views and columns with the below select statement.
SELECT COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%email%'
ORDER BY TableName,ColumnName;
So that I get something like the below
|ColumnName |TableName |
|emailAddress |all_emails |
....
But I want to get something like this:
|ColumnName |TableName |Example |
|emailAddress |all_emails |first.last#gmail.com|
....
Can anyone offer any insight?
I can't think of a simple way to do this within a query, but here's one option...
Put the list of the columns and tables into a temp table and run them through a loop, using dynamic SQL to select the max row for each.
I've added plenty of comments below to explain it.
DECLARE #SQL NVARCHAR(1000)
DECLARE #TABLE NVARCHAR(1000)
DECLARE #COLUMN NVARCHAR(1000)
DECLARE #SAMPLE NVARCHAR(1000)
DROP TABLE IF EXISTS ##TABLELIST
SELECT COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
,ROW_NUMBER() OVER (ORDER BY COLUMN_NAME,TABLE_NAME)[RN]
INTO ##TABLELIST
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%email%';
ALTER TABLE ##TABLELIST
ADD [Sample] NVARCHAR(1000) -- Add a column for your sample row.
DECLARE #ROWCOUNTER INT = 1 -- Add a counter for the loop to use.
WHILE #ROWCOUNTER <= (SELECT MAX([RN]) FROM ##TABLELIST) -- Keep the loop running until the end of the list.
BEGIN
UPDATE ##TABLELIST
SET #TABLE = TableName WHERE [RN] = #ROWCOUNTER -- Get the table name into a variable.
UPDATE ##TABLELIST
SET #COLUMN = ColumnName WHERE [RN] = #ROWCOUNTER -- Get the column name into a variable.
SET #SQL = 'SELECT #SAMPLE = MAX([' + #COLUMN + ']) FROM [' + #TABLE + ']' -- Create SQL statement to pull max column from table specified in variables.
EXEC SP_EXECUTESQL #SQL, N'#SAMPLE NVARCHAR(1000) OUTPUT', #SAMPLE OUTPUT -- Execute SQL and put the output into the #SAMPLE variable.
UPDATE ##TABLELIST
SET [Sample] = CAST(#SAMPLE AS NVARCHAR(1000)) WHERE [RN] = #ROWCOUNTER -- Insert the SQL output into the sample column.
SET #ROWCOUNTER = #ROWCOUNTER+1 -- Add one to the row counter to move to the next column and table.
END
SELECT * FROM ##TABLELIST -- Select final output.

SQLserver Store Column as variable and loop through it

I am still pretty new to SQL server and I am not sure how to do this. I am first creating a table with just the IDs I need:
SELECT DISTINCT
ID_NUMBER
INTO
#IDlist
FROM
V_Rpt_IDs WITH (NOLOCK)
WHERE
ID_NUMBER in (
'1000764169'
,'1005870537'
,'1008053856'
,'1008054376'
,'1008410224'
,'1008411317'
,'1008465318'
,'1008466074'
,'1008492967'
,'1010546872'
,'1010554301')
Select * from #IDlist
And this works fine. But now I would like to declare a variable to represent this column, or each item in this column, so that I can then do a loop where it loops through each ID Number and returns information about each one and then presents all of that as a table. Here is my shot at that:
Declare #IDNumber as VARCHAR(10)
Set #IDNumber = #IDlist.ID_NUMBER
DECLARE #cnt INT = 0
WHILE #cnt < (Select Count(*) From #IDlist)
BEGIN
SELECT TOP 1
NAME
,MAILING_ADDRESS_1
,MAILING_ADDRESS_CITY
,MAILING_STATE
,MAILING_ZIP
from
V_Rpt_Info
WHERE
ID_NUMBER = #IDNumber
SET #cnt = #cnt + 1
END
DROP TABLE #IDlist
But when I Set the #IDNumber variable to #IDlist.ID_NUMBER, it says The multi-part identifier "#IDlist.ID_NUMBER" could not be bound.
How do I do this?
Thanks
The way you set the variable is not correct, SQL doesn't know which ID_NUMBER row it should assign to the #IDNumber variable.
You should do this with a SELECT, for example
SET #IDNumber = SELECT TOP 1 ID_NUMBER FROM #IDlist
But, why would you like to loop through this temporary table this way ? Isn't it possible to join the necessary data with this table instead of doing it one by one ?
Rather then loop through, you're going to want to join your ID table to your V_Rpt_Info view.
SELECT
NAME
, MAILING_ADDRESS_1
, MAILING_ADDRESS_CITY
, MAILING_STATE
, MAILING_ZIP
FROM V_Rpt_Info V
INNER JOIN #IDlist ID
ON V.ID_NUMBER = ID.ID_NUMBER

SQL Cursor returning many number of tables

I have a sql stored proc where I use a cursor that contains a set of id's from a select statement and I use these id's going over one by one using the cursor to get values into other variables and use those variables to do sql joins .My problem is when I execute this I get many tables returned whereas I need just one table returned.
SET NOCOUNT ON
declare #BSVal as int
declare #GSVal as int
declare #mID as int
declare #qID as int
DECLARE M_Cursor cursor for
select
ms.MID,ms.QID
from vM As ms join QS as qs
ON ms.QSID=qs.QIDjoin
Mar as mar on mar.MarID=qs.MarID
where (ms.Cid='Web')
open M_Cursor
FETCH NEXT FROM M_Cursor
INTO #mID, #qID
--Get values
WHILE ##FETCH_STATUS = 0
BEGIN
set #BSVal= (select top 1 SCID from vSC where (EnID in
(select EnID from En where EnName='BAIDU')
and QTID=1 and MID=#mIDand QSID=#qID)order by ITime desc);
set #GSVal= (select top 1 SCID from vSC where ( EnID in
(select EnID from En where EnName='GRAPHIC') and QTID=1
and MID=#mIDand QSID=#QSID) order by ITime desc);
select * from
vM m
join vw5TABLE BNDCG on (m.QSid=BNDCG.QID And BNDCG.Position=1)
join vw5TABLE GNDCG on (m.QSid=GNDCG.QID And GNDCG.Position=1)
where
BNDCG.SCid=#BSVal
and GNDCG.SCid=#GSVal
and BNDCG.QSID=# qID
and GNDCG.QSID=# qID
and m.MID=#mID
FETCH NEXT FROM M_Cursor
INTO #MID, #QSID
END
CLOSE M_Cursor;
DEALLOCATE M_Cursor;
That code will run a select for each iteration of the cursor, which makes it look like 'many tables'. It sounds like you need to insert the results of that select into a temp table or table variable inside the cursor, then once the cursor is complete, select once from that temp table. I have not gone over your code in detail but I'm guessing a cursor may not be required for this.
Here's a rough sample using a table variable.
DECLARE #temptable TABLE (col1 INT, Col2 VARCHAR(3), Col3 INT)
insert into #temptable (col1,col2,col3)
select (col1,col2,col3) from
vM m
join vw5TABLE BNDCG on (m.QSid=BNDCG.QID And BNDCG.Position=1)
join vw5TABLE GNDCG on (m.QSid=GNDCG.QID And GNDCG.Position=1)
where
BNDCG.SCid=#BSVal
and GNDCG.SCid=#GSVal
and BNDCG.QSID=# qID
and GNDCG.QSID=# qID
and m.MID=#mID
....
..
DEALLOCATE M_Cursor;
SELECT Col1,Col2,Col3 FROM #temptable

Using Arrays in SQL

I have this sql statement:
DECLARE #option_id INT;
SELECT DISTINCT product_options.option_id
INTO #option_id
FROM product_options,
product_options_descriptions
WHERE product_options.product_id = '31288'
AND product_options_descriptions.option_name = "Color";
SELECT #option_id;
Which works fine. What I would like to do is is use #option_id to select multiple ids into an array. However as far as I can tell DECLARE #var only works with single values and I can't find any information on array datatypes in sql.
In outline I want to do the following
declare #option_id;
SELECT #option_id;
DECLARE #id_array;
SELECT into #id_array WHERE id = #option_id;
DECLARE #return_array;
FOREACH #id IN #id_array {
#return_array[] = SELECT value FROM column where id = #id
}
SELECT #return_array
Does anyone know where I can find tutorials ect to achieve this?
You can use temporary table , table prefixed with #
CREATE TABLE #YourTable (
YourColumn int
)
LInk : http://www.sqlteam.com/article/temporary-tables
For mysql syntax : http://dev.mysql.com/doc/refman/5.1/en/create-table.html

How to dynamically write the query in SQL Server 2008?

How to write the dynamically the below query?
Table
empid designation interestes
1 developer,tester cricket,chess
1 developer chess
1 techlead cricket
Condition:
IF empid = 1
AND (designation LIKE '%developer%' OR designationLIKE '%techlead%')
OR (interests LIKE '%cricket%').
How to write the above query dynamically if designations need to send more than 2,and also same on interstes .
please tell me ...
EDIT stored procedure code:
ALTER PROCEDURE [dbo].[usp_GetDevices]
#id INT,
#designation NVARCHAR (MAX)
AS
BEGIN
declare #idsplat varchar(MAX)
set #idsplat = #UserIds
create table #u1 (id1 varchar(MAX))
set #idsplat = 'insert #u1 select ' + replace(#idsplat, ',', ' union select ')
exec(#idsplat)
Select
id FROM dbo.DevicesList WHERE id=#id AND designation IN (select id1 from #u1)
END
Then when your form is submitted, create a string of designations (should really be a list of foreign keys if you have a 1 to many relationship) and pass that to the SQL. Then parse it into a table using one of many open-source SQL user functions:
-- #designations = 'developer,tester,techlead'
select text_val
from dbo.fn_ParseText2Table(#designations,',')
/* results:
text_val
--------
developer
tester
techlead
*/
Once you have the values in a table you can do any standard join or query operations.