質問

I'm trying to run following code

USE [cp_mydatabse]
GO

ALTER Procedure [dbo].[SP_myProcedure]
AS
BEGIN
   IF EXISTS (SELECT name FROM master.sys.databases WHERE name = 'tempDB')
      EXEC SP_UpdatetempDB - copy data from another data base
   END
   ELSE
   BEGIN
       print 'database does not exists'
       EXEC SP_CreatetempDB -- create my database SP
       EXEC SP_CreatetempDBTables - create table SP
       EXEC SP_UpdateTEmpDBData - copy data from another database
   END

If my tempDB exists, I figured out that code is running the EXEC SP_CreatetempDB as well. What is the reason for that ? But I cannot see

print 'database does not exists'

output on my console. Do you have any idea about this ?

Thanks in advance.

役に立ちましたか?

解決

Try this:

ALTER Procedure [dbo].[SP_myProcedure]
AS
IF EXISTS (SELECT name FROM master.sys.databases WHERE name = 'tempDB') BEGIN
 EXEC SP_UpdatetempDB - copy data from another data base
END
ELSE BEGIN
  print 'database does not exists'
  EXEC SP_CreatetempDB -- create my database SP
  EXEC SP_CreatetempDBTables - create table SP
  EXEC SP_UpdateTEmpDBData - copy data from another database
END

他のヒント

i am naive to sql server & database .....

i think

USE [cp_mydatabse]
GO
ALTER Procedure [dbo].[SP_myProcedure]
AS
BEGIN
IF EXISTS (SELECT name FROM master.sys.databases WHERE name = 'tempDB')
BEGIN
EXEC SP_UpdatetempDB - copy data from another data base
END
ELSE
BEGIN
print 'database does not exists'
EXEC SP_CreatetempDB -- create my database SP
EXEC SP_CreatetempDBTables - create table SP
EXEC SP_UpdateTEmpDBData - copy data from another database
END
END

Should work

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top