Solved

How to run comma separated values in loop

Posted on 2013-12-31
8
229 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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