Solved

How to run comma separated values in loop

Posted on 2013-12-31
8
230 Views
Last Modified: 2013-12-31
Dear Experts,
Need your help
The below mentioned query need to run three times since @FeeType is carrying three string (1,4,5)
Please help me


Declare @FeeType Varchar(100)
Set @FeeType='1,4,5'


select @FeeAmount = (select FeeAmount From ClassesFee Where ClassId = @ClassId and FeeTypeId = @FeeTypeId and FiscalYearID=@FiscalYearID and CampusId=@CampusId)

Insert Into S(StudentName,@FeeAmount)
Select StudentName,@FeeAmount from Students
0
Comment
Question by:Mehram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39749447
If you want to insert FeeAmount for all of students then you could write:
Declare @FeeType Varchar(100)
Set @FeeType='1,4,5'

Insert Into S (StudentName, FeeAmount)
SELECT S.StudentName, CF.FeeAmount FROM dbo.Students S CROSS JOIN 
(SELECT   FeeAmount
    FROM     ClassesFee
    WHERE    ClassId = @ClassId
            AND @FeeTypeId LIKE '%' + CAST(FeeTypeId AS VARCHAR(8)) + '%'
            AND FiscalYearID = @FiscalYearID
            AND CampusId = @CampusId) CF

Open in new window

0
 

Author Comment

by:Mehram
ID: 39749456
Sir,
I have select only three types of fee and need to create fee against those three type of all student.

The appended query is generating fee against all fee type what ever in classesFee.
need to review query to select only provided fee types (@FeeType='1,4,5')


Declare @FeeType Varchar(100)
Set @FeeType='1,4,5'

Create Table s (StudentName Varchar(100), FeeAmount int)
Insert Into S (StudentName, FeeAmount)
SELECT S.StudentName, CF.FeeAmount FROM dbo.Students S CROSS JOIN
(SELECT   FeeAmount
    FROM     ClassesFee
    WHERE    ClassId = '1'
            AND @FeeType LIKE '%' + CAST(FeeTypeId AS VARCHAR(8)) + '%'
            AND FiscalYearID = '1'
            AND CampusId = '1') CF
Select * from s
Drop Table s
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39749462
Are you sure? the following condition should restrict the result to those with FeeTypeID of 1,4,5:
WHERE @FeeType LIKE '%' + CAST(FeeTypeId AS VARCHAR(8)) + '%'

Open in new window

then the cross join will create the desired result, assume we have two students (A and B) and three FeeAmounts (10, 40, 50):
A 10
A 40
A 50
B 10
B 40
B 50
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:Mehram
ID: 39749465
Sir student in table is 614 result of above query generating 3927 rows, while its need to generate 1842 rows. Since i have selected three types of fee (614*3)
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39749472
strange result, 3927 / 614 = 6.4 (!!!)
could you please run the following query and tell me the output:
Declare @FeeType Varchar(100)
Set @FeeType='1,4,5'

SELECT   FeeAmount
FROM     ClassesFee
WHERE @FeeType LIKE '%' + CAST(FeeTypeId AS VARCHAR(8)) + '%'

Open in new window


what about this one:
SELECT   FeeAmount
FROM     ClassesFee
WHERE FeeTypeId IN (1,4,5)

Open in new window

0
 

Author Comment

by:Mehram
ID: 39749476
SELECT   FeeAmount
FROM     ClassesFee
WHERE FeeTypeId IN (1,4,5) and ClassId='1'

result Three rows
Result
0
 

Author Comment

by:Mehram
ID: 39749477
Result of this query is also same as above three rows

Declare @FeeType Varchar(100)
Set @FeeType='1,4,5'

SELECT   FeeAmount
FROM     ClassesFee
WHERE @FeeType LIKE '%' + CAST(FeeTypeId AS VARCHAR(8)) + '%'
and ClassId='1'
0
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 500 total points
ID: 39749492
fine, then the query should work.
I created sample tables and it worked, I have no idea why you query doesn't work.
CREATE TABLE #ClassesFee(
	[ID] [int] NOT NULL,
	[FeeTypeId] [int] NOT NULL,
	[FiscalYearID] [int] NOT NULL,
	[CampusId] [int] NOT NULL,
	[ClassId] [int] NOT NULL,
	[FeeAmount] [int] NULL)
	
CREATE TABLE #Students(
	[ID] [int] NOT NULL,
	[StudentName] [varchar](100) NOT NULL)
	
INSERT #ClassesFee ([ID], [FeeTypeId], [FiscalYearID], [CampusId], [ClassId], [FeeAmount]) VALUES (1, 1, 1, 1, 1, 1000)
INSERT #ClassesFee ([ID], [FeeTypeId], [FiscalYearID], [CampusId], [ClassId], [FeeAmount]) VALUES (2, 4, 1, 1, 1, 600)
INSERT #ClassesFee ([ID], [FeeTypeId], [FiscalYearID], [CampusId], [ClassId], [FeeAmount]) VALUES (3, 5, 1, 1, 1, 0)

INSERT #Students ([ID], [StudentName]) VALUES (1, 'A')
INSERT #Students ([ID], [StudentName]) VALUES (2, 'B')

SELECT * FROM #Students
SELECT * FROM #ClassesFee

Declare @FeeType Varchar(100)
Set @FeeType='1,4,5'

CREATE TABLE S(StudentName VARCHAR(100), FeeAmount INT)

INSERT INTO S (StudentName, FeeAmount)
SELECT S.StudentName, CF.FeeAmount FROM #Students S CROSS JOIN 
(SELECT   FeeAmount
    FROM     #ClassesFee
    WHERE    ClassId = '1'
            AND @FeeType LIKE '%' + CAST(FeeTypeId AS VARCHAR(8)) + '%'
            AND FiscalYearID = '1'
            AND CampusId = '1') CF
SELECT * FROM S
DROP TABLE S
DROP TABLE #Students
DROP TABLE #ClassesFee

Open in new window

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question