0

I am trying to insert a row if date_start (type datetime) is in past and date_start+duration(type; real) (gets the end date.) is in future. I keep getting 'more than one result returned from sub query.

IF  (CAST(CONVERT(datetime,(SELECT date_start FROM [tableA])) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float))<0 
AND 
(24*(CAST(CONVERT(datetime, (SELECT date_start FROM [tableA])) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float)) + (SELECT duration FROM [tableA]))>0
BEGIN
        INSERT INTO [tableB](col1) 
    select 24*(CAST(CONVERT(datetime,date_start) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float))  FROM [tableA]
END

Any idea how can i do this?

3 Answers 3

1

@Fearghal you should try this -

DECLARE @required_date DATETIME
DECLARE @duration REAL

DECLARE date_cursor CURSOR FOR
SELECT date_start, duration FROM [tableA]

OPEN date_cursor
FETCH NEXT FROM date_cursor
INTO @required_date, @duration 
WHILE @@FETCH_STATUS = 0
BEGIN
    IF  (CAST(@required_date as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as    float))<0 
AND 
(24*(CAST(@required_date as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float)) + @duration)>0
    BEGIN
         INSERT INTO [tableB](col1) 
         select 24*(CAST(CONVERT(datetime,date_start) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float))  FROM [KAP_db_C4].[dbo].[PM]
    END
FETCH NEXT FROM date_cursor
INTO @required_date, @duration
END
CLOSE date_cursor
DEALLOCATE date_cursor
Sign up to request clarification or add additional context in comments.

6 Comments

Il give that a go tomorrow and get back to you :)
That nearly worked! :) The only problem now is that it inserts everything every time it finds a match for if statement, which is my fault for not stating this in questions originally. I only need it to insert the result of the calc for the matching line - 24*(CAST(CONVERT(datetime,date_start) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float)) FROM [tableA]
I added the conditions for the if statement to the insert statment to make sure only the row that satisfy the if are inserted into the table but alas it still inserts all. insert...from tableA where 24*(CAST(CONVERT(datetime,date_start) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float)) FROM [tableA] but no luck, it still inserted all the rows!
you mean to say datetime return from Select statement in insert and @required_date should be same?
Oh!! I thought you used two different table names yesterday. So i used cursor. If it was a single table then it is easier and your answer is correct. Cheers!!
|
1

That would be because of this one:

SELECT duration FROM [tableA]

I'm quite sure based off your error that needs filtered so that one row gets returned.

4 Comments

If only one row is required, use TOP 1 in your select
@Thefunkymonkey, that's probably not the right criteria. I mean it could be, but it's probably not.
Yea but i want to check all rows for start<0 and start_duration>0? I cant just check 1 row?
@Fearghal, you can't check all of them at the same time when you're trying to add the duration returned to another value. You need to either put this inside a CURSOR, or completely restructure how you're getting at the data so it's done via a SELECT.
0

Ok think i got it.... correct me if im wrong but the cursor and variable stuff is not needed. I ended up with this. I couldnt have done it with ya, cheers

INSERT INTO [TABLEB](col1,........, col6) 
         select 24*(CAST(CONVERT(datetime,date_start) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float)), ENTITY, 'PM', NULL, NULL, NULL, NULL, '0'  FROM [TABLEA] 
            where (CAST(CONVERT(datetime,date_start) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float))<0 
            AND 
            (24*(CAST(CONVERT(datetime,date_start) as float)- CAST(CONVERT(datetime,CURRENT_TIMESTAMP) as float)) + duration)>0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.