Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to run comma separated values in loop

Posted on 2013-12-31
8
Medium Priority
?
236 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 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