views:

85

answers:

4

I have create a stored procedure with VB.NET

This procedure goes to a remote Server (for the moment on my near server Win 2003) until now everything are ok

The procedure is created and i call her from my program

The name is sp_AddStreamDB

mycommand = new sqlcommand("EXEC sp_AddStreamDB 'C:\sqlDATA\', 'RemoteDB'",RemoteSQLConn)
myCommand.ExecuteNonQuery()

Normally the procedure will take the two parameters and she put them in there own right places to executed, but that is not Happen

CREATE PROCEDURE [dbo].[sp_AddStreamDB] (
     -- Add the parameters for the stored procedure here
     @DPath varchar(max)
    ,@DBName varchar(50)
    ,@Qchar varchar(1) = "'"
    )
AS 
    BEGIN_TRY:
    SET QUOTED_IDENTIFIER ON ;
    SET NOCOUNT ON ;
    -- Insert statements for procedure here
    DECLARE @ErrMsg nvarchar(4000)
       ,@DBName1 varchar(50)
       ,@DBName2 varchar(50)
       ,@DBNamefs varchar(50)
       ,@DBNamelog varchar(50)
       ,@FileGroupN varchar(100)
       ,@DATName varchar(max)
       ,@LOGName varchar(max)
       ,@FSName varchar(max)

    SET @DBName1 = ( @DBName + '1' )
    SET @DBName2 = ( @DBName + '2' )
    SET @DBNamefs = ( @DBName + 'fs' )
    SET @DBNamelog = ( @DBName + 'log' )
    SET @FileGroupN = ( @DBname + 'StreamGroup' )
    SET @DATName = ( @Qchar + @DPath + @DBName + '_dat.mdf' + @Qchar )
    SET @LOGName = ( @Qchar + @DPath + @DBName + '_log.ldf' + @Qchar )
    SET @FSName = ( @Qchar + @DPath + @DBName + '_fs' + @Qchar )

    BEGIN_CATCH:
    SELECT  ERROR_MESSAGE() AS ErrorMessage ;
    SELECT  @ErrMsg = ERROR_MESSAGE()

    CREATE DATABASE DBName
    ON PRIMARY 
       (
         NAME = DBName1
        ,FILENAME = DATName
       ),
    FILEGROUP FileGroupN CONTAINS FILESTREAM
      (
        NAME = DBNamefs
        ,FILENAME = FSName
       )
    LOG ON
       (
        NAME = DBNamelog
        ,FILENAME = LOGName
        )

    RAISERROR (@ErrMsg,1,1)
    RETURN 0
    END_CATCH:
    END_TRY:

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

CREATE DATABASE is the important issue

This procedure gives me an error "The File Name 'DATName' is not in the right format ....."

The varable @DATName is 'C:\sql\DATA\RemoteDB.dat.mdf' ( i have check this by PRINT @DATName and it is OK

But for some reason the line CREATE DATABASE refused to take the value of the variable and takes just the name (only the name) DATName which tries to excecute the name as value and of course that returns me the error

NOW

IS THERE ANYBODY WHICH CAN ASSIST ME ON THIS ISSUE?

After some troubleshooting tips i've change my code to this:

PRINT @DATName
CREATE DATABASE [@DBName]
ON PRIMARY
   (
     NAME = '@DBName1'
    ,FILENAME = '@DATName'
   ),
FILEGROUP [@FileGroupN] CONTAINS FILESTREAM
  (
    NAME = '@DBNamefs'
   ,FILENAME = '@FSName'
  )
LOG ON 
  (
   NAME = '@DBNamelog'
   ,FILENAME = '@LOGName'
   )

and i'm taking the error: A file activation error occurred. The physical file name '@DATName' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

Of course the @DATName is that: 'C:\sqlDATA\RemoteDB_dat.mdf'

The same i take it when i remove the brackets And the same i take it when i remove the single quotes

Lefteris Gkinis [email protected] 0030 22960 32843

A: 
CREATE DATABASE DBName ON PRIMARY (NAME = DBName1, FILENAME = DATName), FILEGROUP FileGroupN CONTAINS FILESTREAM (NAME = DBNamefs, FILENAME = FSName) LOG ON (NAME = DBNamelog, FILENAME = LOGName)

may be you left the @ in the variables of that command¿

CREATE DATABASE @DBName ON PRIMARY (NAME = @DBName1, FILENAME = @DATName), FILEGROUP @FileGroupN CONTAINS FI

EDIT:

Another solution, more complicated but I'm sure it works (I have used it many times):

  • Dinamicaly create your create database command in VB (all text, without parameters)
  • Run a command from VB to create a new SP in master database with your new fixed parameter in it.
  • Run the new master SP from VB (the database will be created)
  • Run a command from VB to delete the master SP

It is not as smart as your initial solution, but it works.

j.a.estevan
When I put them '@' returns an error as wrong typing the procedure
Lefteris Gkinis
I may be wrong but almost sure that you can't create a database with it's name as a variable.
j.a.estevan
+1  A: 

I'm fairly certain that you can't pass variables to the CREATE DATABASE statement. You'll need to construct an entire CREATE DATABASE within a varchar variable and then pass that to sp_executesql.

JDHnz
I put an answer to my Question because as comment was too longI will try this Dear JDHnz
Lefteris Gkinis
Well Dear JDHnzYou are the only one which gives me the perfect solutionnow i'm facing a new issueThis issue i will discribed in a new TopicPlease follow it
Lefteris Gkinis
A: 

Dear j.ja.estevan

After many retries with various combinations now i'm turned to your place i see that this command (CREATE DATABASE) can't works with variable from insitde of a procedure

Because from vb.net works just fine

And if there is a such of problem now i'm coming to the reason of why i use stored procedure

If i use the CREATE DATABASE from inside the code then he can't understood my path C:\sqlDATA\RemoteDB_dat.mdf

I mean he things that i'm trying to created in my own PC (or what else)

Because it turns back the message Access Deny when i'm tring to write the file RemoteDB_dat.mdf

And i'm interpriter this message that he (the sql server) to run the command from iside of his machine

So When i put my real path and names to the stored procedure then he bypass all the previous problems and gives me the last error (of Access deny)

So we are in same place where we have start it

!@#$%^& any Help? !@#$%^&*

Lefteris Gkinis
Editted with a new solution, hope this helps.
j.a.estevan
A: 

This works over here

CREATE PROCEDURE [dbo].[sp_AddStreamDB] (
     @Path nvarchar(500)
    ,@DBName nvarchar(120)
    )
AS 
    SET NOCOUNT ON
    BEGIN TRY

        DECLARE @File1Logical nvarchar(120)
           ,@File1Physical nvarchar(500)
           ,@StreamGroup nvarchar(120)
           ,@StreamLogical nvarchar(120)
           ,@StreamPhysical nvarchar(500)
           ,@LogLogical nvarchar(120)
           ,@LogPhysical nvarchar(500)
           ,@exe nvarchar(4000)
           ,@ErrMsg nvarchar(2048)


        SET @File1Logical = @DBName + N'1'
        SET @File1Physical = @Path + +@DBName + N'_dat.mdf'
        SET @LogLogical = @DBName + N'log'
        SET @LogPhysical = @Path + @DBName + N'_log.ldf'
        SET @StreamGroup = @DBName + N'fs'
        SET @StreamLogical = @DBname + N'StreamGroup'
        SET @StreamPhysical = @Path + @DBName + N'_fs'

        SET @exe = N'
        CREATE DATABASE <db_name_here>
        ON PRIMARY 
            (
              NAME = <file1_logical_here>
             ,FILENAME = ''<file1_physical_here>''
             ),
        FILEGROUP <streamgroup_here> CONTAINS FILESTREAM
            (
              NAME = <stream_logical_here>
             ,FILENAME = ''<stream_physical_here>''
             )
        LOG ON 
             (
               NAME = <log_logical_here>
              ,FILENAME = ''<log_physical_here>''
              )'

        SET @exe = REPLACE(@exe, N'<db_name_here>', @DBName)
        SET @exe = REPLACE(@exe, N'<file1_logical_here>', @File1Logical)
        SET @exe = REPLACE(@exe, N'<file1_physical_here>', @File1Physical)
        SET @exe = REPLACE(@exe, N'<log_logical_here>', @LogLogical)
        SET @exe = REPLACE(@exe, N'<log_physical_here>', @LogPhysical)
        SET @exe = REPLACE(@exe, N'<streamgroup_here>', @StreamGroup)
        SET @exe = REPLACE(@exe, N'<stream_logical_here>', @StreamLogical)
        SET @exe = REPLACE(@exe, N'<stream_physical_here>', @StreamPhysical)

        EXEC sp_executesql @exe
    END TRY
    BEGIN CATCH
        SET @ErrMsg = ERROR_MESSAGE()
        RAISERROR (@ErrMsg,1,1)
        RETURN 0
    END CATCH
    RETURN 1

To test:

DECLARE @RC int

EXECUTE @RC = [dbo].[sp_AddStreamDB]  @Path =N'c:\db\',@DBName =N'SomeDB'
SELECT @RC

The c:\db\ directory must exists and sql service account must have read/write permissions on it.

Damir Sudarevic
I'll do thatBut tell me thisHave you Checked this in your own computer?and if you have done this did you run it in the SQL server which run in your own pc? or in an SQL server which runs remotely?
Lefteris Gkinis
Remotely as in Management Studio on one PC, and server on another.
Damir Sudarevic
Well I settle your instructions When i run this procedure inside of my code returns me nothing (looks like all is well)And the instruction myCommand.ExecuteNonQuery() returns -1But the Database not created nothing (remember that all the neccessary prerequsites are there and all are well)
Lefteris Gkinis
I put your test code inside of the server and i run it then again nothing happen but i receive the well known error:DECLARE @RC intEXECUTE @RC = [dbo].[sp_AddStreamDB] @Path =N'c:\sqlDATA\',@DBName =N'RemoteDB'SELECT @RC------------------------*/Unable to open the physical file "c:\sqlDATA\RemoteDB_fs". Operating system error -2147024891: "0x80070005(Access is denied.)". as for an account i use an SQL account with all the privelige he needs for administer the disk
Lefteris Gkinis
I tested again, works here. Try: make sure that `c:\SQLDATA` directory exists and set FULL CONTROL for EVERYONE on it. Make sure that `c:\sqlDATA\RemoteDB_fs` does not exists.
Damir Sudarevic
Also note the error `Operating system error ... (Access is denied.)` this has nothing to do with SQL code -- there is something wrong with permissions of the SQL Server Service account on the directory.
Damir Sudarevic
i have full control for everyone and the c:\sqlDATA\ is there prior to run and the Remote_fs is not existsAnd yes i see that now the Access denied is not something from SQL but it is coming from the Windows system and that is what i'm trying to see nowBut if there is anything you have in your mind please send it
Lefteris Gkinis
YEs YEs Yes YesNow I know qhat is going on First you need to SET the command CREATE DATABASE and all this to send it on the sp_executesqland the other is to change the 'SQL Server (SQLEXPRESS)' service on the remote server from the 'NT AUTHORITY\NETWORK SERVICE' log on to a Local System account or somone else which you use as AdministratorBe very carefull the 'log on as' is giving it by default from the instalation of the SQL in the NT AUTHORITY\NETWORK SERVICEAnd all those only in case you need to Create a Stream Data Base in a remote serverTHANK YOU VERY MUCH I hope will meet again
Lefteris Gkinis