|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, February 21, 2013 7:38 AM
Points: 211,
Visits: 378
|
|
Hi All
I am just after a bit of clarification. I am writing a procedure which inserts data from one table (Source) into another (Destination) using certain criteria. (E.g. People Aged 50+)
insert into destination select * from source where age>49 I then want to set a variable using the numbers of records I have just appended into this table (Destination) - select @totalrecs=count(*) from destination
The Next step is to peform a calculation (2000-[no of records inserted into destination table])
select @totalrecs=2000-@totalrecs Then I want to insert into the destination, using the result of the calculation,
insert into destination select top @totalrecs from source where [criteria] order by newid() Now I am trying to do all this in one fluent procedure - could someone advise me of the best way to do this?
I am currently attempting to do this in the following manner:
declare @sql nvarchar(1000), @totalrecs int
set @sql='insert into source select * from destination where age>49
select @totalrecs=count(*) from destination set @totalrecs=2000-@totalrecs
insert into destination select top '+CONVERT(NVARCHAR(10),@TOTALRECS)+' * from source where age<50' Print @sql Exec(@sql) Nothing is returned to the screen (from the print command) and the query is not executed - Anybody have an idea why and maybe suggest a better way to perform this
Thanks
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Yesterday @ 6:52 AM
Points: 325,
Visits: 315
|
|
Well, it's not printing anything because @totalrecs is NULL when you concatentate it, and NULL concatenated with anything else yields NULL. You will need to assign @totalrecs a value. Why don't you just move all this into a stored procedure rather than trying to build a string and then execute it?
Roland Alexander The Developing World
There are two means of refuge from the miseries of life: music and cats. ~ Albert Schweitzer
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 1:07 PM
Points: 6,826,
Visits: 11,951
|
|
bicky1980 (1/2/2013)
I am currently attempting to do this in the following manner: declare @sql nvarchar(1000), @totalrecs int
set @sql='insert into source select * from destination where age>49
select @totalrecs=count(*) from destination set @totalrecs=2000-@totalrecs
insert into destination select top '+CONVERT(NVARCHAR(10),@TOTALRECS)+' * from source where age<50' Print @sql Exec(@sql) Nothing is returned to the screen (from the print command) and the query is not executed - Anybody have an idea why and maybe suggest a better way to perform this Thanks One other item of note, @totalrecs is not going to be available as a variable inside your dynamic SQL. A batch executed using EXEC (or sys.sp_executesql) is in a different scope when it comes to variables than the scope from which it is called. You can see what I mean by running this:
DECLARE @sql NVARCHAR(MAX), @totalrecs INT;
SET @sql = 'SELECT @totalrecs AS [@totalrecs];'; PRINT @sql; EXEC(@sql); You'll receive this output:
SELECT @totalrecs AS [@totalrecs]; Msg 137, Level 15, State 2, Line 1 Must declare the scalar variable "@totalrecs".
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 8:37 PM
Points: 2,370,
Visits: 3,252
|
|
bicky1980 (1/2/2013)
Hi All I am just after a bit of clarification. I am writing a procedure which inserts data from one table (Source) into another (Destination) using certain criteria. (E.g. People Aged 50+) insert into destination select * from source where age>49 I then want to set a variable using the numbers of records I have just appended into this table (Destination) - select @totalrecs=count(*) from destination The Next step is to peform a calculation (2000-[no of records inserted into destination table]) select @totalrecs=2000-@totalrecs Then I want to insert into the destination, using the result of the calculation, insert into destination select top @totalrecs from source where [criteria] order by newid() Now I am trying to do all this in one fluent procedure - could someone advise me of the best way to do this? I am currently attempting to do this in the following manner: declare @sql nvarchar(1000), @totalrecs int
set @sql='insert into source select * from destination where age>49
select @totalrecs=count(*) from destination set @totalrecs=2000-@totalrecs
insert into destination select top '+CONVERT(NVARCHAR(10),@TOTALRECS)+' * from source where age<50' Print @sql Exec(@sql) Nothing is returned to the screen (from the print command) and the query is not executed - Anybody have an idea why and maybe suggest a better way to perform this Thanks
Here's my two cents worth:
1. As an earlier responder pointed out, this doesn't need to be dynamic SQL because you can run:
insert into destination select top (expr) * from source where age<50'
Where expr is a query like:
select @totalrecs=2000-count(*) from destination
2. What do you expect to happen when more than 2000 records are INSERTed into destination in the first query? In other words, 2000-count(*) is negative.
No loops! No CURSORs! No RBAR! Hoo-uh!
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
Need to UNPIVOT? Why not CROSS APPLY VALUES instead? Since random numbers are too important to be left to chance, let's generate some! Are you too recursively challenged? Splitting strings based on patterns can be fast!
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, February 21, 2013 7:38 AM
Points: 211,
Visits: 378
|
|
2. What do you expect to happen when more than 2000 records are INSERTed into destination in the first query? In other words, 2000-count(*) is negative.
Sorry- The first command should have said select top 2000
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, February 21, 2013 7:38 AM
Points: 211,
Visits: 378
|
|
Roland Alexander STL (1/2/2013) Well, it's not printing anything because @totalrecs is NULL when you concatentate it, and NULL concatenated with anything else yields NULL. You will need to assign @totalrecs a value. Why don't you just move all this into a stored procedure rather than trying to build a string and then execute it?
Surely @totalrecs would be the figure which is remaining??
declare @totalrecs int
select @totalrecs=999 set @totalrecs=2000-@totalrecs print @totalrecs
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, April 16, 2013 9:40 PM
Points: 14,
Visits: 49
|
|
Maybe you can Try this
INSERT INTO DESTINATIONTABLE(TOTALCOUNT) SELECT COUNT(FIELD) FROM SOURCETABLE WHERE AGE = 49
|
|
|
|