How do I create a dataset in SQL Server and process each record?
Clash Royale CLAN TAG#URR8PPP
How do I create a dataset in SQL Server and process each record?
I have the following table:
CREATE TABLE [dbo].[proposalReviewAction]
(
[proposalReviewActionID] [INT] IDENTITY(1,1) NOT NULL,
[proposalPackageID] [INT] NULL,
[approvalTypeID] [INT] NULL,
[comments] [VARCHAR](2000) NULL,
[reviewedByID] [INT] NULL,
[reviewedDate] [DATETIME] NULL
) ON [PRIMARY]
I want to return each record that has an approvalTypeID = 100
and use the proposalPackageID
in another process that moves the data from the proposal table to the project table.
approvalTypeID = 100
proposalPackageID
I considered using a WHILE
loop, but everyone seems to favor processing a dataset but I cannot find any way to do this in SQL/T-SQL.
WHILE
I found an example of a WHILE
loops and modified it ...
WHILE
WHILE (SELECT[approvalTypeID] FROM [proposalReviewAction]) = 100
BEGIN
DECLARE @ppID AS INT ;
SELECT [proposalPackageID] AS ppID, [approvalTypeID] AS atID
FROM [proposalReviewAction]
IF (SELECT [approvalTypeID] AS atID FROM [proposalReviewAction]) = 100
PRINT @ppID
ELSE
BREAK
END
PRINT 'I got to the print statement';
... but I get the following error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Then, it prints the "PRINT" statement.
Any help is appreciated...
Bob
@SeanLange, what alternative would you suggest?
– R Loomas
2 hours ago
I would suggest a set based approach. Forget the loop unless you really need it. You say something about processing the row but that is unclear what that means.
– Sean Lange
2 hours ago
Can you be more specific about what you want to do with the
ppID
values?– Joel Coehoorn
2 hours ago
ppID
@JoelCoehoorn, the ppID value will correspond to the proposalPackageID in the proposal table. I want to use the ppID to pull the data from the proposal table and insert it into the project table. I only want to process the proposal records with an approvalTypeID = 100 (which represents an "approval to fund."
– R Loomas
2 hours ago
1 Answer
1
Given this table:
CREATE TABLE [dbo].[proposalReviewAction]
(
[proposalReviewActionID] [INT] IDENTITY(1,1) NOT NULL,
[proposalPackageID] [INT] NULL,
[approvalTypeID] [INT] NULL,
[comments] [VARCHAR](2000) NULL,
[reviewedByID] [INT] NULL,
[reviewedDate] [DATETIME] NULL
) ON [PRIMARY]
You can select all records with approvalTypeID = 100
by using a simple SELECT statement
approvalTypeID = 100
SELECT * FROM dbo.proposalReviewAction WHERE approvalTypeID = 100;
If you only need proposalPackageID
then you can SELECT just it using the QUERY
proposalPackageID
SELECT proposalPackageID FROM dbo.proposalReviewAction WHERE approvalTypeID = 100;
Depending on the processing you could insert directly from here to the 'project table' as well
INSERT INTO [projectTable] (proposalPackageID ...)
SELECT proposalPackageID, ...
FROM dbo.proposalReviewAction WHERE approvalTypeID = 100;
I think you missed the part where I said, "I want to return each record that has an approvalTypeID = 100 and use the proposalPackageID in another process that moves the data from the proposal table to the project table." I've got the list, I just need to loop through and process each record.
– R Loomas
18 mins ago
This is returned as a list and then you can do whatever you want with them indivually or on whole. You also need to state what kind of processing you want to do for more explicit help. Cursors and WHILE loops are expensive in a DB and depending on the processing, you may need to do that in the application. There is a reason people favor set-based processing in a DB.
– Daniel Gale
9 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The error message is pretty clear. Your subquery returns more than 1 row and you trying to see if equals 100. How that can be evaluated if there are multiple rows? I have to agree that doing this in a loop is a poor way to do whatever it is you are doing. Sql should be done set based, not RBAR (row by agonizing row).
– Sean Lange
2 hours ago