Setting a variable with multiple rows in SSIS [duplicate] - sql

This question already has answers here:
Which is the best way to form the string value using column from a Table with rows having same ID?
(2 answers)
Closed 7 years ago.
I need to do something like this in SSIS:
From one SQL table I need to get some id values, I am using a simple sql query:
Select ID from Identifier where value is not null.
I've got this result:
As a final result I need to generate and set a variable in SSIS with the final value:
#var = '198','120','ACP','120','PQU'
Which I need to use later in a odbc expression.
Is this possible in SSIS?
Just to clarify: The image is just a little example of what I can get from the first part of the process. I mean, the number of ID that I need is unknown.

Use simple query
DECLARE #TEST TABLE(ID NVARCHAR(10))
INSERT INTO #TEST VALUES('186'), ('120'), ('ACP'), ('120'), ('PQU')
DECLARE #ID VARCHAR(8000)
SELECT #ID = CONCAT(COALESCE(#ID + ',''', ''''), ID, '''')
FROM #TEST
SELECT #ID
Result
'186','120','ACP','120','PQU'

Related

How to DECLARE variable and set values to a field in a table [duplicate]

This question already has answers here:
SQL Server store multiple values in sql variable
(7 answers)
Closed 2 months ago.
I am trying to set values but need to use a field, rather than inputting hundreds of values.
Current code:
DECLARE #variable AS VARCHAR (100)
SET #variable = 'Y'
I need to be able to use a field as the value:
SET #variable = tbl.field
I have also tried
DECLARE #variable AS table (val varchar (100))
insert into #variable (val)
(SELECT
distinct field
FROM
tbl])
select * from #variable
SELECT * FROM tbl
WHERE field = #variable
However this code simply runs both at the same time, creating two outputs, so I am missing a link here
I need to be able to run the code so that all available values are set as each option needs to be tested at once.
You declared a scalar variable. It holds only one value, and cannot hold more than one value.
In this approach, you can store multiple values.
DECLARE #variable AS TABLE(val VARCHAR(50))
INSERT INTO #variable(val) VALUES
('Y'),
('N')
SELECT * FROM #variable
As Larnu wrote, you can't assign a scalar value to hold two values.
My suggestions are using a temporary table to hold all of your values, or assign both values as one and break them with STRING_SPLIT.
For example:
DECLARE #variable VARCHAR(100) = 'Y,N'
SELECT value
FROM STRING_SPLIT(#variable, ',')
A scaler variable only can store a single value in each set/select statement.

SQL Server. Allow multiple values throwing error Conversion failed when converting the nvarchar value … to data type int

Newbie here. Attempting to allow report to run multiple values when being run but above error happens when selecting multiple. Any help will be appreciated.
where ContactActivityNoteType.ClientId = 1
and ContactActivityNoteType.ContactActivityNoteTypeId in (13, 4, 22)
and tc.ContactTypeId = 2
and ca.EnteredOn between #start_date and #end_date
and ca.ClientAccountId in (#ClientAccountId)
One option is to change your multi-valued parameter to a temporary table and then join that temp table to your main table.
In your query based dataset, you can do this part before main query.
For example, if following is your multi-valued variable:
DECLARE #ClientAccountId VARCHAR(MAX) = 'a10001,a10002,a10003,a10004'
You can use the following chunk of SQL code to change it to a temp table:
CREATE TABLE #ClientAccountIds (ClientAccountId VARCHAR(30));
DECLARE #Insert VARCHAR(MAX) = 'INSERT INTO #ClientAccountIds VALUES ('''+REPLACE(#ClientAccountId,',','''),(''')+''');';
EXEC (#Insert);
So that following SQL:
SELECT ClientAccountId FROM #ClientAccountIds
will return:
And now you can easily join this temporary table to your main query.
Hope this help?

T-SQL Identity Seed Expression [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to use an expression for identity seeds in T-SQL?
We're putting a bunch of tables together, using BIGINT for the ID columns, but want to stagger the seeds so that the tables are not overlapping their ID number spaces.
This is much easier to do using hex values, so we can mask out the highest two bytes or so, such as seed 1 * 0x1000000000000 for table1 and seed 2 * 0x1000000000000 for table2 etc. - thereby still leaving plenty of possible IDs available for each table.
The problem here is, SQL doesn't like seeing the hex values or multiplication in the IDENTITY statement, so we tried manually casting them to BIGINT but the error persisted: "incorrect syntax, expected INTEGER or NUMERIC."
It seems like T-SQL doesn't want to see anything other than a single literal value, in decimal (not hex), with no math operations.
We can deal with this, by doing the math ourselves and converting the numbers to decimal - but we'd like to avoid this if possible, since the numbers are more difficult to keep track of in decimal format - bug prone, etc.
(I should explain, bug-prone, because we use these values to determine which table an object belongs to, based solely on it's ID value being in the appropriate number space - those first two bytes being a sort of "table ID")
However, is there another way to accomplish what I'm describing using hex values and multiplication, while using some weird syntax that T-SQL can accept?
I know this is an inconvenience, not a blocking issue, but I want to make sure there truly aren't any alternatives before we settle on this workaround.
Just blend bad ideas by using dynamic SQL:
declare #Bar as BigInt = 0x1000000000000;
declare #Foo as NVarChar(1000) = 'dbcc checkident(''Foo'', RESEED, ' + cast( 2 * #Bar as NVarChar(64) ) + ')';
exec sp_executesql #Foo;
I'm sure a RegEx would improve it.
Create a trigger (before insert) for this table, and disable identity.
Convert Hex to int: Convert integer to hex and hex to integer
Example:
CREATE TRIGGER TRIGGER_NAME
ON TABLE_NAME
INSTEAD OF INSERT
AS
BEGIN
IF (SELECT ID FROM INSERTED) IS NULL
BEGIN
DECLARE #INITIAL_ID BIGINT = (SELECT CONVERT(BIGINT, 0x1000000000000) * 2)
DECLARE #NEW_ID BIGINT =
ISNULL( (SELECT MAX(ID) FROM TABLE_NAME) + 1, #INITIAL_ID )
SELECT * INTO #INSERTED FROM INSERTED
UPDATE #INSERTED SET ID = #NEW_ID
INSERT INTO TABLE_NAME SELECT * FROM #INSERTED
END
ELSE
BEGIN
INSERT INTO TABLE_NAME SELECT * FROM INSERTED
END
END
GO
When you create the table you can set the initial seed value.
create table Table1(Id int Identity(10000000,1), Name varchar(255));
or you can use this statement on a table that is already created
DBCC CHECKIDENT ('dbo.Table1', RESEED, 20000000);
The next entry will be 20000001 assuming you have the step = 1

Return Comma separated values SQL [duplicate]

This question already has answers here:
How to make a query with group_concat in sql server [duplicate]
(4 answers)
Closed 9 years ago.
I have a field names DAILYREPORT.WEATHERCONDITION which can hold values as '1,2,3' or '1' or '2,4' based on what the user selects from the list of weather check boxes available to him. The weather table contains the list of weathers which he selects
Weather Table
ID Condition
----------
1 Sunny
2 Cloudy
3 Fine
4 Windy
Now i need a query which returns the conditions as 'Sunny,Cloudy,Fine' when DAILYREPORT.WEATHERCONDION=1,2,3
Try this :
SELECT STUFF
(
(select ',' + Condition
from
Weather
where
ID in (1,2,3)
FOR XML PATH('')
),1,1,''
)
DECLARE #list VARCHAR(MAX)
SELECT #list = COALESCE(#list+',' ,'') + Condition
FROM Weather
WHERE ID IN (1,2,3)
SELECT #list
You declare a #list variable of varchar type. Then using the COALESCE expression (please look here http://msdn.microsoft.com/en-us/library/ms190349.aspx for further details on how it works) you get what you want.
That's a SQL fiddle that show that the above works as it is expected
http://sqlfiddle.com/#!6/65df2/1
Note : In order to avoid any misconception, I don't say that the COALESCE solves the stated problem.
It is is only there to deal with initializing the string and the issue
of a extra comma at the end.
as Mikael wrote below.
mine is same as krishna,
Declare #Weather Table (ID int, Condition varchar(50))
insert into #Weather values(1,'Sunny'),(2,'Cloudy'),(3,'Fine'),(4,'Windy')
select top 1
stuff((select ','+Condition from #Weather b where id in(1,2,3) for xml path('')),1,1,'')Condition
from #Weather
Try this i hope it is useful to Your
select SUBSTRING(
(select ','+ s.condition from DAILYREPORT s where id in (1,2,3) order by condition for xml path('')),2,200000) as CSV

SELECT [...] WHERE X IN #variable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Parameterizing an SQL IN clause?
I have read-only access to a database which I'd like to do the following on:
DECLARE #var AS INT_COLLECTION = (1,2,3)
SELECT name,column
FROM table
WHERE column IN #var
Of course INT_COLLECTION doesn't exist, but is there something similar available in SQL Server 2008? Or another way to do this without write access to the database?
This is a pretty heavy-handed approach since you have to create a user-defined table type, but it does work:
CREATE TYPE INT_COLLECTION AS TABLE (
Value Int
);
GO
DECLARE #var AS INT_COLLECTION;
INSERT INTO #var (Value)
VALUES (1), (2), (3);
SELECT name, col
FROM YourTable
WHERE col IN (SELECT Value FROM #var)
You could do something like this:
DECLARE #var AS TABLE (IDS INT);
INSERT INTO #var VALUES (1),(2),(3);
SELECT name,column
FROM table
WHERE column IN ( SELECT IDS FROM #var)