Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please check the select statement below and correct it .

The user select and process one record at a time.

----------------------------------------------------------------------------------
TABLE :: KOANS

IDNO   LOANUMBER NUMDAYS  REPAY 
-------------------------------------
BEN     001        2       SIMPLE
AMY     002        0       REDUCING
ANN     003        1       HOLD 
BET     004        0       
JON     005        5       SIMPLE


Please validate the codes below

------------------------------------------------------------------------------------
SQL
IF NOT EXISTS(SELECT * FROM KOANS WHERE  (ISNULL(NUMDAYS,0)  = 0)
 	                              OR (REPAY NOT IN ('SIMPLE','REDUCING'))
 	                              AND IDNO=@ZIDNO AND LOANUMBER= @ZLOANUMBER)
	BEGIN
  	SET @TYEAR=@CYEAR
	END
ELSE
    BEGIN
       RAISERROR('There are incomplete records the process has been  Aborted', 11, 1) 
   END


----------------------------------------------------------------------------------

The following records should return the error message when selected

TABLE :: KOANS

IDNO   LOANUMBER NUMDAYS  REPAY 
-------------------------------------
AMY     002        0       REDUCING
ANN     003        1       HOLD 
BET     004        0       



Please correct the SQL statement .

Thanks

What I have tried:

Tried my old codes and coudn't get it
Posted
Updated 10-Mar-16 11:20am
v2
Comments
AnvilRanger 10-Mar-16 15:07pm    
Another "Do my homework question"?
Member 10744248 10-Mar-16 15:19pm    
please this is not home work please
RedDk 10-Mar-16 16:12pm    
Sure but I'm missing some basic information:

DECLARE @ZLOANNUMBER (?) ... lets assume this is "int(410)"

&

DECLARE @ZIDNO (?) ... and assume this is "int(13)"

Yes?

Does your select statement work? SELECT * FROM KOANS WHERE (ISNULL(NUMDAYS,0) = 0)
OR (REPAY NOT IN ('SIMPLE','REDUCING'))
AND IDNO=@ZIDNO AND LOANUMBER= @ZLOANUMBER
Substitute real values in for @ZIDNO and @ZLOANNUMBER and get that too work. then work on the NOT EXISTs
 
Share this answer
 
Here's what I have tried:
USE [cpqaAnswers]
GO
CREATE TABLE [cpqaAnswers].[cpqa].[KOANS](
	[IDNO] nvarchar(13),
		[LOANUMBER] nvarchar(14),
			[NUMDAYS] nvarchar(13),
				[REPAY] nvarchar(23)
				)

/*
   content of KOAN.txt (tab delimited text):


        BEN     001        2       SIMPLE
        AMY     002        0       REDUCING
        ANN     003        1       HOLD 
        BET     004        0       
        JON     005        5       SIMPLE
*/	

BULK INSERT [cpqaAnswers].[cpqa].[KOANS] FROM 'C:\cpqaAnswers\cpqa_ST_varDECLARE_160310-1315\KOAN.txt'

SELECT * FROM [cpqaAnswers].[cpqa].[KOANS]

/*
	IDNO	LOANUMBER	NUMDAYS	REPAY
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	BEN	001		2	SIMPLE
	AMY	002		0	REDUCING
	ANN	003		1	HOLD 
	BET	004		0	NULL
	JON	005		5	SIMPLE
*/

Something's wrong starting here ->
DECLARE @ZIDNO nvarchar(13)
DECLARE @ZLOANUMBER nvarchar(14)
DECLARE @TYEAR nvarchar(20)
DECLARE @CYEAR nvarchar(410)

And I get an error message (see beneath) here:
IF NOT EXISTS(SELECT * FROM [cpqaAnswers].[cpqa].[KOANS] WHERE  (ISNULL(NUMDAYS,0)  = 0)
 	                              OR (REPAY NOT IN ('SIMPLE','REDUCING'))
 	                              AND IDNO=@ZIDNO AND LOANUMBER= @ZLOANUMBER)
	BEGIN
  	SET @TYEAR=@CYEAR
	END
ELSE
    BEGIN
       RAISERROR('There are incomplete records the process has been  Aborted', 11, 1) 
   END

Text of message:

Msg 50000, Level 11, State 1, Line 14
There are incomplete records the process has been  Aborted

Does any of this like similar to anything else I should be seeing?
 
Share this answer
 
Comments
CHill60 10-Mar-16 18:33pm    
Is this meant to be a solution?
RedDk 10-Mar-16 19:55pm    
What?
A couple of things, I do not think that it is causing an error but you do not need to DECLARE on every line. One DECLARE and a comma in between the variables.
I never see you set your variables @ZIDNO and @ZLOANNUMBER. Where are you setting @CYear??? Is this going to be a stored procedure that you are going to pass variables?
Here are the steps that I would do.
1 Create Table, you have done this.
2 get the select statement working, I do not think that this is working correctly
SELECT *
FROM [KOANS]
WHERE (ISNULL(NUMDAYS, 0) = 0)
OR (
REPAY NOT IN (
'SIMPLE'
, 'REDUCING'
)
)
AND IDNO = 'Jon'
AND LOANUMBER = '005'
What are you trying to bring back from the Select statement?

3.Replace your variables with values, then run it. you should have two records one that would not exist in your select statement and one that would exists and return your error message.
 
Share this answer
 
Comments
CHill60 10-Mar-16 18:35pm    
Did you know that you can use the Improve solution link to add extra information to your original solution. It's far better than posting an extra solution and far less confusing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900