Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert statement not working - require scalar expression


Insert statement not working - require scalar expression

Author
Message
wardy
wardy
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 23
Hi, I am after a little help with an insert statement which is giving me an error of 'Subqueries are not allowed in this context. Only scalar expressions are allowed.'

I am not that clued up on scalar expressions and after hours of google search I am none the wiser.

Bascialy I am trying to insert 2 column entries into a table, incomekey and flightkey. However the income key field is defined as a char (8) not null, no auto increment has been built in. so as a work around, I am trying to do a search to find the last used number in the field and add 1 to that value hence the select MAX statement

insert into sbsincome (incomekey,flightkey) values ((select MAX(incomekey)+1 from sbsincome),116040)



If i just run the select statement without the insert it works fine. How can I get the increment value to work on my insert statement?

thank you in advance
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14973 Visits: 38989
the issue is to NOT use the INSERT...VALUES syntax, but INSERT...SELECTinstead:


insert into sbsincome (incomekey,flightkey)
select
MAX(incomekey)+ 1 AS incomekey,
116040 AS flightkey
from sbsincome



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

steveb.
steveb.
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2856 Visits: 7195
you will need to break the logic out into seperate steps

1. define a variable to hold the value,
2. select the max(incomekey) into this variable.
3. use this variable in your insert statement

one issue you will have when using this approach for incrementing keys, is that if two or more requests are happening at the same time there will be a possibility of dupliacate keys occuring.
wardy
wardy
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 23
Thanks Lowell your example works,


I also note the warning provided by SteveB. I understand this and I am hoping this is just temporary until the company that support us can actually change the database structure.

Cheers Guys for your speedy response.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search