How to save all the output in snowflake procedure? - sql

I have written a procedure wherein a variable has a select statement which is creating multiple update statements..
If I execute the variable it will give the query from select statement and if I execute the variable which has all the update statement list will it update everything as required??
I also want to know how can I save all each update query's row count as there are more than 300 updates statements

Related

Using UPDATE statement while using multiple WITH

I have a sql statement that returns records that I need and I am using several WITH clauses. I am trying to update one column within the statement.
WITH <name_a> AS (SELECT...),
<name_b> AS (SELECT...using 'name_a'),
<name_c> AS (SELECT...using 'name_b')
SELECT * from <name_c>
How would I write the statement if I just want to update a column in the final <name_c> output? Ex.
UPDATE name_c SET fav_color = 'blue'
for all the records.
I tried just replacing the select with the update statement but I get 'Missing SELECT keyword'.
I found a similar question but it looks like this question focused on updating the value to what was returned from the select statement. Using "WITH" and "UPDATE" statements in the same SQL query

Executing stored procedure in a loop until a condition is met using SSIS

I have a customers table whose fields have to be updated with recent changes and also an additional column in the same table which specifies if that particular row has been updated or not, I have a procedure that does this for me
CustomerID CustomerName Address Updated
1 abc xyz Yes
2 cdb tyy No
As you can see above , the second row hasn't been updated due to NO connection to the client server.Later when the connection to server is available, all I have to do is update the column value to blank and run the procedure again till the row is updated with any changes.
I have to flow this process in a loop until all rows in the table are updated.
Could someone please help me out to do this in SSIS
The following will allow your stored procedure run until all the rows have been updated. A couple important things to note, if it's possible that there will be one or more iterations where no rows in the table will have been updated, a condition, such as a T-SQL IF statement, will need to be added to the second Execute SQL Task to check for this and assign a value to the variable accordingly. Also, the parameter syntax (using ? for the parameter place holder) is for an OLE DB connection.
Add a For Loop container, in the EvalExpression field, select an int variable and set the condition as the variable not equal to 0, i.e. #[User::Count] != 0
Within the For Loop container, add an Execute SQL Task that executes the stored procedure
Add another Execute SQL Task linked to the first one and still inside the For Loop, that has a query which returns the count of rows from the table without the updated value that will be mapped to the variable from the For Loop, such as
select ? = count(*) from dbo.YourTable where Updated != 'Yes'
On the second Execute SQL Task, go to the Parameter Mapping pane, pick your variable in the Variable Name field, set the Direction to Output, Data Type to Long, and Parameter Name to 0. The Parameter size can remain at the default of -1.

How to re-use a SQL query in a PL/SQL procedure?

I am writing a PL/SQL procedure. In the body of this procedure, how can I use twice the same query without re-writing it ?
To simplify, let's say that I have this SQL query :
SELECT *
FROM mytable
WHERE age > 18
Is there a way to "store it", so I could do for example :
SELECT COUNT(*) INTO var1
FROM myQuery
I know the WITH ... AS keywords, but as I know it can be only used in the current statement, and I want to be able to call it from different statements.
Thanks !
There are various possibilities. Here are the ones I think of immediately, there are probably others:
Declare an explicit CURSOR using your query, and use that cursor multiple times in the body of your procedure.
Store the query in a string variable, and use EXECUTE IMMEDIATE to run it multiple times
Execute the query once, storing the results in a local collection (nested table, most likely), and process those stored results multiple times
Create a function that executes the query and returns its results as a nested-table type. Then SELECT FROM TABLE( my_function ) multiple times

executing query inside column in a table

I have a table A with columns 1.Column 1 contains a query select * from tableD;
I need to execute the query inside column1 using single query if possible..How to do this..
Database is netezza.If u provide ans for oracle Db also fine.
This seems Dynamic query problem for me ..
In Oracle you can create a procedure to call this query which is actually stored in a table.
In my knowledge you can not directly run query stored as text.
You need to create a procedure or function where you can call this query as dynamically with execute immediate command.

How to use IF condition for column selection in sql server 2008 R2

In my project I want data from same table in 2 different manner.
1) Only 5 fields
2) Only 10 fields
And I am getting the data through Stored Procedure for each one. But I want that if possible I should make one procedure.
So, can I put if condition for selecting column?
For example, if I pass parameter as "Less", it will get data of only 5 columns and if I pass parameter as "More", it will get data of 10 columns. I can have two SELECT statement in procedure based on condition(that I have already done) but I want to make it One SELECT statement. Is it possible?
No, is it not possible to have a single SELECT statement.
But then, you can do this:
if #ColumnList = 'less'
begin
call storedProc_returning5Columns
end
else
begin
call storedProc_returning10Columns
end
[Another option is to use Dynamic TSQL]
This isn't possible.
Your current solution of having an IF statement in the stored procedure is the best approach.