Solved

How to run comma separated values in loop

Posted on 2013-12-31
8
221 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
  • 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
 

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

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, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now