Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to store multiple values in one variable like array or list in sql.how to do that.

What I have tried:

SQL
SET @SYSRECORD=Sales A/c,Bank Accounts,Bank O/D Account,Cash-in-hand,Duties & Taxes,Expenses (Direct),Expenses (Indirect)


it is not working

i have tried this also

SQL
INSERT INTO @ListofIDs ('a'),('b'),('b'),('c'),('d'),('d');
SELECT IDs FROM @ListofIDs  AS names
Posted
Updated 30-Mar-16 20:32pm
v2
Comments
jaket-cp 30-Mar-16 4:44am    
Can you give an example on how you would like to use the list.

Don't.
Instead, have a separate table which uses a foreign key back to the original, and contains the value you want to select:
SalesStaff
ID    Name
1     Joe
2     Mike

Sales
Id    SalesId   Value
1     1         100
2     1         200
3     2         300
And then use and SQL Join to "tie them together"
SQL
SELECT ss.ID, ss.Name, s.Value FROM Sales s
JOIN SalesStaff ss
ON ss.ID = s.SalesID 

That gives you:
1     Joe       100
1     Joe       200
2     Mike      300
While it is possible to store multiple values in a single column but converting them to strings and comma separating them, it's a PITA to work with and always gives problems. For example, what happens if you want to delete the "500" from "1,2,330,500,5000,200,5"? It's a real pain!
 
Share this answer
 
You are looking for table variables[^].
SQL
DECLARE @ListofIDs TABLE (id varchar(10))

INSERT INTO @ListofIDs ( id )
VALUES  ('a'),('b'),('b'),('c'),('d'),('d');

SELECT id AS [name] FROM @ListofIDs
 
Share this answer
 
Comments
aarif moh shaikh 29-Mar-16 7:50am    
+5
i got my solution.
i used this code which creates temporary table to store multiple value.when we run our code it displays that table.

DECLARE @data TABLE (grp VARCHAR(100))



INSERT INTO @data --values('Capital Account','Loan')
SELECT 'Capital Account'
UNION ALL
SELECT 'Current Liabilities'
UNION ALL
SELECT 'Current Assets'
UNION ALL
SELECT 'Fixed Assets'
UNION ALL
SELECT 'Profit & Loss'
UNION ALL
SELECT 'Revenue Account'
UNION ALL
SELECT 'Suspense Account'
UNION ALL
SELECT 'Loan';
SELECT grp FROM @data;
 
Share this answer
 
Comments
jaket-cp 31-Mar-16 4:12am    
If that is all you need to do, then you do not need to insert into a temp variable table. Unless you are using the @data TABLE in multiple places.

Just do the select with union all and give the first column an alias of grp.
SELECT 'Capital Account' grp
UNION ALL
...
Member 12385326 31-Mar-16 4:20am    
i am not getting it..can you show it with my example?
jaket-cp 31-Mar-16 4:23am    
As in your sql code.
Remove delcare, insert and select from @data table.
Add in grp for the select statement, like below:

SELECT 'Capital Account' grp
UNION ALL
SELECT 'Current Liabilities'
UNION ALL
SELECT 'Current Assets'
UNION ALL
SELECT 'Fixed Assets'
UNION ALL
SELECT 'Profit & Loss'
UNION ALL
SELECT 'Revenue Account'
UNION ALL
SELECT 'Suspense Account'
UNION ALL
SELECT 'Loan';
Member 12385326 31-Mar-16 4:30am    
but i am using this data like this
IF @abc='insert' and @Chbox_Val='p' and @Group_Name IN (SELECT grp FROM @data)

begin
insert into tbl_AccountGroup(Group_Name,Chbox_Val ,UnderGroup,Comments,flag )
values(@Group_name,@Chbox_Val,@UnderGroup,@Comments,'1')
set @Group_id= SCOPE_IDENTITY();
print 'block1'
end

for this i need to store this values in list kind of thing..so used this table.am i going write or wrong?
jaket-cp 31-Mar-16 4:37am    
if that is what you are doing, then you could also do it like this:
@Group_Name IN ('Capital Account','Current Liabilities','Current Assets','Fixed Assets','Profit & Loss','Revenue Account','Suspense Account','Loan')
OR
@Group_Name IN (
SELECT 'Capital Account'
UNION ALL
SELECT 'Current Liabilities'
UNION ALL
SELECT 'Current Assets'
UNION ALL
SELECT 'Fixed Assets'
UNION ALL
SELECT 'Profit & Loss'
UNION ALL
SELECT 'Revenue Account'
UNION ALL
SELECT 'Suspense Account'
UNION ALL
SELECT 'Loan'
)


But I suppose it can be maintained more easily when placed stored in the @data table.

Hope that helps you out.

But on second thought, it would be better if you create a permanent Group lookup type table. As suggested in solution 1 oops meant solution 2.

For example if a new Group_Name was required, then all you need to do is insert it into the Group lookup table rather than modifying the sql code to add the extra group into the list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900