Hello!
I have a stored procedure that takes a file name as a parameter and
imports the data from there into the appropriate table using the
correct format file. I would like to save the number of rows inserted
at the end of my stored procedure. Since the file name is variable I
use EXEC (@.cmd) where the @.cmd is a BULK INSERT command that I have
generated.
Since some of these files are extremely large I have set the BATCHSIZE
for BULK INSERT to 1,000,000. However, this causes @.@.ROWCOUNT to only
show the number of rows inserted in the last batch. Since the table
name is variable I don't have an easy way of getting the count(*) from
it.
Any suggestions?
Thanks!
-Tom.Aardvark (tom_hummel@.hotmail.com) writes:
> I have a stored procedure that takes a file name as a parameter and
> imports the data from there into the appropriate table using the
> correct format file. I would like to save the number of rows inserted
> at the end of my stored procedure. Since the file name is variable I
> use EXEC (@.cmd) where the @.cmd is a BULK INSERT command that I have
> generated.
> Since some of these files are extremely large I have set the BATCHSIZE
> for BULK INSERT to 1,000,000. However, this causes @.@.ROWCOUNT to only
> show the number of rows inserted in the last batch. Since the table
> name is variable I don't have an easy way of getting the count(*) from
> it.
> Any suggestions?
You could run SELKCT COUNT(*) on the table before and after, but that's
of course ont very appealing on a large table. (The fact that the
table is dynamic should not be a problem. You do know sp_executesql,
don't you? Else read
http://www.sommarskog.se/dynamic_sq...#sp_executesql.
The other option would be to have the client to catch those rows
affected messages for each batch committed.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland Sommarskog wrote:
> You could run SELKCT COUNT(*) on the table before and after, but that's
> of course ont very appealing on a large table. (The fact that the
> table is dynamic should not be a problem. You do know sp_executesql,
> don't you? Else read
> http://www.sommarskog.se/dynamic_sq...#sp_executesql.
> The other option would be to have the client to catch those rows
> affected messages for each batch committed.
Thanks for the advice Erland. I had forgotten that sp_executesql
allowed for output parameters, so that should give me what I need using
SELECT COUNT(*). Not the most elegant way and it will suffer a
performance hit since some of the tables can be quite large, but it's a
process that runs for up to 12 hours once a month, so a couple minutes
to get a count from a table shouldn't be a problem.
Thanks!
-Tom.
Showing posts with label rowcount. Show all posts
Showing posts with label rowcount. Show all posts
Tuesday, March 27, 2012
Sunday, February 26, 2012
Get result from EXEC()
I currently do this:
INSERT INTO #tbl2 EXEC(@.sql)
IF @.@.ROWCOUNT = 1
INSERT INTO @.tbl3 SELECT userID FROM #tbl2
DELETE FROM #tbl2
Is there any other way to recieve the result from column userid from the @.sql-query?Hi,
try to use sp_executesql (see BOL):
sp_executesql [@.stmt =] stmt
[
{, [@.params =] N'@.parameter_name data_type [,...n]' }
{, [@.param1 =] 'value1' [,...n] }
]
Markus|||How do you mean?|||Hi,
i don't know what you really want to query, but here an (quick and dirty) example:
-- testing enviroment
create table usernames( userid int, username varchar(100) )
insert into usernames (userid,username) values ( 123, 'moby' )
insert into usernames (userid,username) values ( 986, 'lars' )
-- select * from usernames
--
-- vars
declare @.Stmt nvarchar(200), @.UserID int, @.UserName varchar(100)
-- SQL-Stattement to get result from
select @.Stmt='select @.P1= userid from usernames where username=@.P2'
-- "input" parameter
select @.Username='moby'
-- query result
exec sp_executesql @.Stmt, N'@.P1 int output, @.P2 varchar(100)', @.P1=@.UserID output, @.P2=@.Username
-- show result
print @.Username
print @.UserID
-- get rid of testdata
drop table usernames|||Thank you!
INSERT INTO #tbl2 EXEC(@.sql)
IF @.@.ROWCOUNT = 1
INSERT INTO @.tbl3 SELECT userID FROM #tbl2
DELETE FROM #tbl2
Is there any other way to recieve the result from column userid from the @.sql-query?Hi,
try to use sp_executesql (see BOL):
sp_executesql [@.stmt =] stmt
[
{, [@.params =] N'@.parameter_name data_type [,...n]' }
{, [@.param1 =] 'value1' [,...n] }
]
Markus|||How do you mean?|||Hi,
i don't know what you really want to query, but here an (quick and dirty) example:
-- testing enviroment
create table usernames( userid int, username varchar(100) )
insert into usernames (userid,username) values ( 123, 'moby' )
insert into usernames (userid,username) values ( 986, 'lars' )
-- select * from usernames
--
-- vars
declare @.Stmt nvarchar(200), @.UserID int, @.UserName varchar(100)
-- SQL-Stattement to get result from
select @.Stmt='select @.P1= userid from usernames where username=@.P2'
-- "input" parameter
select @.Username='moby'
-- query result
exec sp_executesql @.Stmt, N'@.P1 int output, @.P2 varchar(100)', @.P1=@.UserID output, @.P2=@.Username
-- show result
print @.Username
print @.UserID
-- get rid of testdata
drop table usernames|||Thank you!
Subscribe to:
Posts (Atom)