Solved

How to run comma separated values in loop

Posted on 2013-12-31
8
228 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linked Server Issue with SQL2012 3 33
Loop through SQL parameters and insert to temp table? 4 53
SQL Recursion 6 33
point in time restore in SQL server 26 43
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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