How the result set of the following query in sql task can be assigned to a variable in ssis - sql

Even after the below configuration , im not able to assign a query result to a variable

In the first form you have specified the ResultSet type as single row, and in the SQL Command you have wrote a SELECT query that may returns multiple rows.
You have to change the SQL Command, and add a TOP 1 keyword
SELECT TOP 1 ... FROM ... WHERE ...
Also if it still not working, in the Result Set Yab, try replacing the columns names with relevant indexes.
MailAddressFrom >> 0
MailAddressTo >> 1
Subject >> 2
If you are looking to store all rows returned by a SELECT Query, you should select FullResultSet option and store the result inside an Object variable:
SSIS Basics: Using the Execute SQL Task to Generate Result Sets

Related

Get the list of result from first sql query and execute second query on the result of list of first query

So, I am working with redshift and SQL for the first time. I have run into this problem due to my limted knowledge of SQL.
I have this first query which return me tables with the columnA. (TableX, TableY... etc)
SELECT tablename
FROM PG_TABLE_DEF
WHERE ("column" = 'columnA');
Also I have this second query which returns me all the rows from the table containig certain value of columnA.
SELECT *
FROM TableX
WHERE columnA='123934'
What I want to achieve is take the result from the first query which is list of tables, and for each table run the second query i.e. get the rows with value of columnA=123934 for each table returned from first query.
What you want to achieve is done using dynamic SQL. Dynamic queries let you run a query from a string.
I am not an Redshit user but to generate the SQL string you need to run you could use the following query:
SELECT 'SELECT * FROM '||tablename ||' WHERE ColumnA= ''123934''; '
FROM PG_TABLE_DEF
WHERE ("column" = 'columnA');
You can try running it manually.

Conditional split satisfying both the conditions

I have sql execute task where it has query like select count(*) from table1, and I am passing the count value to a variable say X. Then I add a data flow task where it has a ole DB source with select * from table1. Then I add a conditional split where the condition is #[User::X] > 0 and #[User::X] == 0. If the count is 0 create a empty file or else create a file with table data.
But here when I execute the package, both empty file and the file with data is getting created.
Where did I go wrong?

How to set multiple SSIS variables in SQL Task

Say I want to get the key values of two tables and assign to two package variables.
I can easily assign one, setting the Execute SQL Task to return a single row. But is it possible to set multiple variables with multiple result sets?
I have this SQL:
SELECT COALESCE(MAX(LogKey),0) AS LogKey FROM Log
SELECT COALESCE(MAX(HeaderKey),0) AS HKey FROM Header
I've tried setting the ResultSet property to both single row and Full result set, but neither seem to work. Do I have to use a separate Execute SQL task?
You could try this to force the values on the same resultset:
SELECT
A = (SELECT COALESCE(MAX(LogKey),0) AS LogKey FROM Log),
B = (SELECT COALESCE(MAX(HeaderKey),0) AS HrKey FROM Header)

Data Manipulation Language with and Without GO statement [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the use of GO in SQL Server Management Studio?
I have some Data Manipulation Language like below.
Case - 1 Without GO
Update Table
Set Columns = 'Value'
Where Id = 1
Update Table
Set Columns = 'Value'
Where Id = 2
Case - 2 With GO
Update Table
Set Columns = 'Value'
Where Id = 1
GO
Update Table
Set Columns = 'Value'
Where Id = 2
Query
Which should be preferred and why ?
The only difference - that the 1st query runs in one turn since 2nd - in tho turns. GO delimiter is not a server command, it is just a batch separator, which should be handled by client.
This means - that 1st query performs better because of 1-turn query
Refer below url it may help you.
http://msdn.microsoft.com/en-us/library/ms188037.aspx which states:
GO is not a Transact-SQL statement; it is a command recognized by the
sqlcmd and osql utilities and SQL Server Management Studio Code
editor.
SQL Server utilities interpret GO as a signal that they should send
the current batch of Transact-SQL statements to an instance of SQL
Server. The current batch of statements is composed of all statements
entered since the last GO, or since the start of the ad hoc session or
script if this is the first GO.
A Transact-SQL statement cannot occupy the same line as a GO command.
However, the line can contain comments.
Users must follow the rules for batches. For example, any execution of
a stored procedure after the first statement in a batch must include
the EXECUTE keyword. The scope of local (user-defined) variables is
limited to a batch, and cannot be referenced after a GO command.
I got the differences in terms of Stored Procedure.
Case 1 - When Select 2 is not the part and explicitly seperated from this stored procedure
Create proc abc
as
select 1
GO
select 2
Case 2 - When Select 2 is not the part of stored Procedure and by mistake became the part of stored procedure due to absence of GO
Create proc abc
as
select 1
select 2
In case 2 , Definition of the language becomes following.
Create proc abc
as
select 1
select 2

How to send record(which is a weblink) from a table to the value of the variable in SSIS package and send that weblink through an email?

I have table called Table1 with columns, col1 and col2 with col1 having weblinks for the report and col2 the name of the report. Now, i have a package with a variables var1 and var2 which should get the col1 and col2 values respectively from table1 and send it through an email. if the weblink gets updated in the table, package should send the updated link. i know the reverse way of it but trying to do somethig like this.
Appreciate any help from you guys.
Thanks
There are a few ways you can do this. You can try one of the following based on whether you are processing one row or many rows.
One-Row Solution
If you are running the SSIS package so that it will process one and only one row from the table, you can use the Execute SQL Task to get the values from your table. Begin by adding your two variables
In the task, specify the connection to your SQL Server and enter a SQL statement like this.
SELECT col1, col2
FROM Table1
WHERE <your condition to retrieve one row>
Also, on the General tab, set the ResultSet value to Single row.
On the Result Set tab, add two elements. The first row should be Result Name of 0, Variable Name of Var1. The second row will be Result Name 1, Variable Name Var2.
When the task runs, the results of the SQL statement will be stored to the variables.
Multi-Row Solution
If the SSIS package must return multiple rows, then you need a different approach. First, you will need to create another variable that is an Object data type. The SQL results will be saved to this variable.
You will have an Execute SQL Task that is similar to the one above. However, your SQL statement should return all rows to be processed. The ResultSet property should be set to Full result set. On the Result Set tab, there should be one result set, with 0 as Result Name and your new object variable as the Variable Name. Close the Execute SQL Task editor.
Add a Foreach Loop Container. Edit the container and select the Collection tab. Set the Enumerator property to Foreach ADO Enumerator. Under the Enumerator configuration, set your object variable as the ADO object source variable.
On the Variable Mappings tab, add to rows. The first row will have a Variable Var1 and Index 0, and the second row will have Variable Var2 and Index 1. The Foreach Loop Container will process each for in the result set and assign the column values to your variable. You can add any tasks in the container and they will run once for each row in the result set.