Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Why doesnt this work............confused!?!?!

Author  Topic 

GavinD1977
Yak Posting Veteran

83 Posts

Posted - 2007-03-21 : 06:56:38

This is an insert statement i'm trying to run. I want it to only insert this recird if the corresponding EMPLOY_REF exists in the EMPLOYEE table. Heres my statement:


INSERT INTO SALHISTY(EMPLOY_REF, SALARY, SAL_REASON, SAL_DATE)
VALUES ('8971','175000.0000','ANNRV','2007-04-01 00:00:00.000')
WHERE '8971' IN (SELECT EMPLOY_REF FROM EMPLOYEE)


This is the error message i'm getting.

Server: Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'WHERE'.


Any help would be greatly appreciated. Thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-21 : 07:00:49
[code]if exists(SELECT * FROM EMPLOYEE where EMPLOY_REF = '8971')
INSERT INTO SALHISTY(EMPLOY_REF, SALARY, SAL_REASON, SAL_DATE)
VALUES ('8971','175000.0000','ANNRV','2007-04-01 00:00:00.000')[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

GavinD1977
Yak Posting Veteran

83 Posts

Posted - 2007-03-21 : 07:03:09
OK, cool...............thanks very much Harsh.

What was it thats wrong with the way i tried to go about it?

Cheers, again :-)
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-21 : 07:09:30
It's in front of you!!

Compare my solution with yours..the form of INSERT statement you are using does not allow WHERE clause to be used.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-21 : 08:11:17
[code]INSERT SALHISTY
(
EMPLOY_REF,
SALARY,
SAL_REASON,
SAL_DATE
)
SELECT EMPLOY_REF,
SALARY,
SAL_REASON,
SALDATE
FROM EMPLOYEE
WHERE EMPLOY_REF = '8971'[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-21 : 08:58:29
INSERT INTO SALHISTY(EMPLOY_REF, SALARY, SAL_REASON, SAL_DATE)
SELECT '8971','175000.0000','ANNRV','2007-04-01 00:00:00.000'
WHERE '8971' IN (SELECT EMPLOY_REF FROM EMPLOYEE)

In any case, this doesn't make much sense





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

GavinD1977
Yak Posting Veteran

83 Posts

Posted - 2007-03-21 : 09:59:35
Guys i have to say thanks very much for the advice.........however I'd also like to take this opportunity to say i feel that some of the responses on here are very abrupt.

The quote from X002548 above sort of highlights this. I am newbie (hence posting in a New To SQL forum) and i posted my question in a polite and friendly manner (also showing my code to highlight the fact i wasnt just asking questions.......but that I had tried to solve this myself first). The repsonse above doesnt help in the slightest! As does the reply by harsh stating "its in front of you".

I am well aware that what I suggested was incorrect and that the reply posted solved my problem. All i was looking for then was someone to explain where and why i'd gone wrong.

If your going to reply to newbie posts I would suggest you maybe take the lack of knowledge into account when replying. trying to learn SQL and getting abrupt posts back doesnt help in the slightest.

Thanks.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-21 : 10:07:35
Fair enough. Perhaps if you post what you are trying to accomplish it would be more helpful...plus you need to have a little thicker skin...in any case..."it's in front of you" means exactl

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-21 : 10:12:26
Do you expect us to provide solution along with the explaination of how it works?

In contrast, we expect you to take some efforts, go through SQL help, google and try to understand the solution. Also, the solution I gave was not very difficult to understand. Besides, I gave you the reason why yours was failing.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-21 : 10:22:47
My suggestion, if you hven't noticed it yet, provides everything for you!
You don't have to put in duplicate information, only the ID of the Employee.

The best thing you can do to learn, is to compare our working suggestions with your own!
Eventually you will see what you did wrong and learn from that.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-21 : 10:28:00
quote:
Originally posted by Peso

My suggestion, if you hven't noticed it yet, provides everything for you!
You don't have to put in duplicate information, only the ID of the Employee.



Provided you have those same columns as a part of EMPLOYEE table and you read OP's mind correctly

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

GavinD1977
Yak Posting Veteran

83 Posts

Posted - 2007-03-21 : 10:32:53
quote:
Originally posted by harsh_athalye


In contrast, we expect you to take some efforts, go through SQL help, google and try to understand the solution. Also, the solution I gave was not very difficult to understand. Besides, I gave you the reason why yours was failing.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"



thats exactly what i did..and i posted my (attempted) code :-s
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-21 : 10:40:22
Gavin,

I am not criticising you and it's good that you made attempt on your own before posting here. All I am saying is try to understand the solutions you get here. May be understanding it in parts will be more easy for you.

After that, if you find some specific part difficult to understand, we can help you out.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

GavinD1977
Yak Posting Veteran

83 Posts

Posted - 2007-03-21 : 11:07:36
OK, cheers harsh.

And genuine thanks to all those who contributed to this.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-21 : 11:18:41
Can we get back to the part of why and what it is you are actually trying to do?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -