Is something like that possible in teradata?
create procedure my_proc(in table_param table)
begin
insert into table1
select *
from table_param;
end;
I believe it is possible in T-SQL
Related
TERADATA SQL, I have SQL statements stored in a table. I want to populate another table with results of those SQL statements. SQL statements are different for each row and each column. Could you please tell me how I can achieve it? I have tried to get results through Stored Procedure and Shell Script. However, I couldn't code it properly.
Here's a pretty barebones procedure for pulling something like this off. It gets a little hairy because:
We are employing a cursor to loop through your source table that holds the SQL Statements
We are employing a second cursor while looping through the first cursor to dynamically execute the SQL String and FETCH the results of that SQL query into a variable.
I would suspect you will run into all sorts of bugs while getting this set up, but I believe this is pretty close to a working procedure:
CREATE PROCEDURE <yourdatbase>.MyNewProcedure()
BEGIN
--Declare variables to hold the three fields as we loop through them with the cursor
--You'll need to change these types to match your table's data type so we can fetch
-- them without error inside the cursor loop
DECLARE customerID INT;
DECLARE customerName VARCHAR(50);
DECLARE sqlQUERY as VARCHAR(10000);
DECLARE source_table_cursor CURSOR FOR SELECT customerID, customerName, SQLQuery FROM <yourdatabase>.yoursourcetable FOR READ ONLY;
OPEN source_table_cursor;
looplabel:
LOOP
--We'll need a cursor for a dynamic statement
--And we'll also need an integer to catch your query
--results (Assuming they are all like (Sum(<somefield>) or some
--type of INT
DECLARE C1 CURSOR FOR S1;
DECLARE sqlResults as INT;
FETCH source_table_cursor INTO customerID, customerName, sqlQuery;
--WOAH THERE! Cursor issues something went wrong die die die
IF (SQLSTATE ='02000') THEN
LEAVE label1;
END IF;
--Now that we have a record and a sql statement we will need another cursor
--where we will execute, dynamically, the SQL statement and then fetch the
--single record result to update our target table
PREPARE S1 FROM sqlQuery;
OPEN C1;
FETCH C1 INTO sqlResults;
--Now we can INsert into your target table
INSERT INTO <yourdatabase>.yourtargettable (customerID, customerName SQL_RESULT)
VALUES (customerID, customerName, sqlResults);
CLOSE C1;
END LOOP looplabel;
CLOSE demographic_cursor;
END
Ho can i define a integer auto increment with oracle 11g?This is my code with mysql user_id int(6) not null auto_increment primary key how can i have this line with oracle?Because i've already the same database in mysql now i want to build the same structure with oracle
You can achieve this with a sequence.
CREATE SEQUENCE seq_user;
The above will auto increment by 1, and start at 1.
To insert values using this sequence, you can do the following (for example):
INSERT INTO table_name (user_id) VALUES (seq_user.NEXTVAL);
To automate this process, you could reference the sequence in a trigger on the table, that adds this value on an insert automatically:
CREATE OR REPLACE TRIGGER user_trg
BEFORE INSERT ON table_name
FOR EACH ROW
DECLARE
BEGIN
IF(inserting)
THEN
:NEW.USER_ID := seq_user.NEXTVAL;
END IF;
END;
i want to filter the rows using Stored procedure. below is my query
Create PROCEDURE [dbo].[Test]
#SearchTerm VARCHAR(100)
AS
BEGIN
Select * from master where name in (#SearchTerm)
END
In code behind,
cmd.Parameters.AddWithValue("#SearchTerm", "peter")
when i run with above parameter, it's work fine.
but when i pass like this
cmd.Parameters.AddWithValue("#SearchTerm", "'peter','rahul'")
this time no rows fetching.
i tried manually then also it's not working.
exec Test ''peter','rahul''
Please help me, how to pass muliple string Using IN clause?
One method is
Create PROCEDURE [dbo].[Test]
#SearchTerm VARCHAR(100)
AS
BEGIN
Select * from master where ','+#SearchTerm+',' like '%,'+name+',%'
You can find more methods at http://www.sommarskog.se/arrays-in-sql-2008.html
I have a cursor in stored procedure. I want to select some data from a query and insert that data into cursor, then again select some data from another query and append that data into the same cursor.
How can I do this?
A cursor is a read-only handle for a SQL statement. A cursor has no data. You cannot append data to a cursor. The only thing you can do with a cursor is fetch the next row.
You can change the SQL statement that is used to open the cursor to UNION together the two different SQL statements, i.e.
OPEN rc FOR
SELECT <<column list>>
FROM table1
UNION ALL
SELECT <<column list>>
FROM table2;
RETURN rc;
i don't know how procedure return values just see this one here 'm using simple ref_cursor
create or replace function test_ref() return sys_refcursor is
temp sys_refcursor;
begin
open temp for 'select * from hr.employees ;
return v_rc;
end;
/
in oracle we have
decalre
v_data number ;
begin
select max(deptno) into v_data from dept;
end;
do we have this type of advatntage of in terdata like selecting the data from table into a variable
can u give the equivalent code in terdata
Thanks
In teradata you can use variables only in stored procedure, you cannot use the variables in Teradata SQl statements which are outside of the stored procedure.
You can load the max value into a temporary table and can access it for your manipulations