Showing posts with label exec. Show all posts
Showing posts with label exec. Show all posts

Friday, March 30, 2012

Running an INSERT statement a @variable without using EXEC

The Code-Piece below does not work because @.Tablename is not an objekt.

=============================================

DECLARE @.Tablename varchar(256)

set @.Tablename = 'MyTable'

insert into @.Tablename default values

=============================================

How can i make it work without using EXEC?

Thanks in advance

Raimund

You have to use dynamic SQL (which is what I presume you mean by using EXEC). I don't think there is any other way.|||If I use scope_identity with an insert-statement by dynamic SQL, scope_identity reurns NULL;|||

If you put the Scope_identity call into the dynamic sql you will be able to capture it to a variable. That variable can then be returned if you use the sp_executesql form of dynamic sql.

|||

You need to call the identity in the dynamic SQL

declare @.tablename varchar(100)

declare @.sql nvarchar(1000)

declare @.i int

set @.tablename = 'mytable'

set @.sql = 'insert into ' + @.tablename + ' default values

set @.i = scope_identity()'

exec sp_executesql @.sql, N'@.i int output', @.i output

select @.i

|||This does mean you can put this code in a UDF|||

Ok. It works fine.

Thanks.

Best Regards

Raimund

Monday, March 26, 2012

running a job

I have a job with 3 steps. Each step has exec sp_start_job
to kick off another job. Will the job step wait 'till th
child job is completed before going to the next step?
TIADid you try it?
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Jon Templar" <anonymous@.discussions.microsoft.com> wrote in message
news:733b01c4cc24$c209fd20$a301280a@.phx.gbl...
> I have a job with 3 steps. Each step has exec sp_start_job
> to kick off another job. Will the job step wait 'till th
> child job is completed before going to the next step?
> TIA|||When you call sp_start_job from another stored proc or an application, the
sp_start_job starts the job and the SQL in the calling SP or application does
not wait for the job to finish, it just keeps going.
I would expect the same behavior for when you exec sp_start_job in each of
the steps in your SQL Job.
To simulate long running jobs, you can use the WAITFOR command.
--e.g., wait for 10 minutes
waitfor DELAY '000:10:00'
Tea C.
"Jon Templar" wrote:
> I have a job with 3 steps. Each step has exec sp_start_job
> to kick off another job. Will the job step wait 'till th
> child job is completed before going to the next step?
> TIA
>

running a job

I have a job with 3 steps. Each step has exec sp_start_job
to kick off another job. Will the job step wait 'till th
child job is completed before going to the next step?
TIA
Did you try it?
http://www.aspfaq.com/
(Reverse address to reply.)
"Jon Templar" <anonymous@.discussions.microsoft.com> wrote in message
news:733b01c4cc24$c209fd20$a301280a@.phx.gbl...
> I have a job with 3 steps. Each step has exec sp_start_job
> to kick off another job. Will the job step wait 'till th
> child job is completed before going to the next step?
> TIA
|||When you call sp_start_job from another stored proc or an application, the
sp_start_job starts the job and the SQL in the calling SP or application does
not wait for the job to finish, it just keeps going.
I would expect the same behavior for when you exec sp_start_job in each of
the steps in your SQL Job.
To simulate long running jobs, you can use the WAITFOR command.
--e.g., wait for 10 minutes
waitfor DELAY '000:10:00'
Tea C.
"Jon Templar" wrote:

> I have a job with 3 steps. Each step has exec sp_start_job
> to kick off another job. Will the job step wait 'till th
> child job is completed before going to the next step?
> TIA
>
sql

running a job

I have a job with 3 steps. Each step has exec sp_start_job
to kick off another job. Will the job step wait 'till th
child job is completed before going to the next step?
TIADid you try it?
http://www.aspfaq.com/
(Reverse address to reply.)
"Jon Templar" <anonymous@.discussions.microsoft.com> wrote in message
news:733b01c4cc24$c209fd20$a301280a@.phx.gbl...
> I have a job with 3 steps. Each step has exec sp_start_job
> to kick off another job. Will the job step wait 'till th
> child job is completed before going to the next step?
> TIA|||When you call sp_start_job from another stored proc or an application, the
sp_start_job starts the job and the SQL in the calling SP or application doe
s
not wait for the job to finish, it just keeps going.
I would expect the same behavior for when you exec sp_start_job in each of
the steps in your SQL Job.
To simulate long running jobs, you can use the WAITFOR command.
--e.g., wait for 10 minutes
waitfor DELAY '000:10:00'
Tea C.
"Jon Templar" wrote:

> I have a job with 3 steps. Each step has exec sp_start_job
> to kick off another job. Will the job step wait 'till th
> child job is completed before going to the next step?
> TIA
>