Script from SQL server database project

I  have created sql server  database  project .I found  I  can  generate  script  using  that (and  also  can  deploy).
But  for  deploying  at  client  side  I  require  mainly  script.When for  trial  I   opened  that  script  in  Sql  server  management  studio  I  found  lot  of  unnessasory  part.And  alos  found  script  does  not  execute  in  management  studio.I  have  attached  that  file.
you can see  in  this entire  file  I  have  only  one  table  Table1  and  nothing  else.I  dont understand  how  to  use this  generated  script.
Datadeploy.publish.sql
ADITYA RAOSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
The script only creates a database and one table. If you want more tables you got to add the code to create them. Also in order to run in MS MS you will need to remove irrelevant code. What is that you are trying to accomplish?
0
ADITYA RAOSoftware DeveloperAuthor Commented:
Have  you  seen  my attached  script file  which  is generated  by  Visual Studio SQL server database  project. In  that  script  you  can see  Creation  of  Table1  which is  only  thing  I  have  created  and  I  want  to be  in  database  created  by  script. But  that  script  does  not  run  in  management  studio.
0
YZlatCommented:
try this (I just removed some VS added code):

/*
Deployment script for Datadeploy

This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/


USE [master];


GO

IF (DB_ID(N'$(DatabaseName)') IS NOT NULL) 
BEGIN
    ALTER DATABASE [$(DatabaseName)]
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    DROP DATABASE [$(DatabaseName)];
END

GO
PRINT N'Creating $(DatabaseName)...'
GO
CREATE DATABASE [$(DatabaseName)]
    ON 
    PRIMARY(NAME = [$(DatabaseName)], FILENAME = N'$(DefaultDataPath)$(DefaultFilePrefix)_Primary.mdf')
    LOG ON (NAME = [$(DatabaseName)_log], FILENAME = N'$(DefaultLogPath)$(DefaultFilePrefix)_Primary.ldf') COLLATE SQL_Latin1_General_CP1_CI_AS
GO
IF EXISTS (SELECT 1
           FROM   [master].[dbo].[sysdatabases]
           WHERE  [name] = N'$(DatabaseName)')
    BEGIN
        ALTER DATABASE [$(DatabaseName)]
            SET ANSI_NULLS ON,
                ANSI_PADDING ON,
                ANSI_WARNINGS ON,
                ARITHABORT ON,
                CONCAT_NULL_YIELDS_NULL ON,
                NUMERIC_ROUNDABORT OFF,
                QUOTED_IDENTIFIER ON,
                ANSI_NULL_DEFAULT ON,
                CURSOR_DEFAULT LOCAL,
                RECOVERY FULL,
                CURSOR_CLOSE_ON_COMMIT OFF,
                AUTO_CREATE_STATISTICS ON,
                AUTO_SHRINK OFF,
                AUTO_UPDATE_STATISTICS ON,
                RECURSIVE_TRIGGERS OFF 
            WITH ROLLBACK IMMEDIATE;
        ALTER DATABASE [$(DatabaseName)]
            SET AUTO_CLOSE OFF 
            WITH ROLLBACK IMMEDIATE;
    END


GO
IF EXISTS (SELECT 1
           FROM   [master].[dbo].[sysdatabases]
           WHERE  [name] = N'$(DatabaseName)')
    BEGIN
        ALTER DATABASE [$(DatabaseName)]
            SET ALLOW_SNAPSHOT_ISOLATION OFF;
    END


GO
IF EXISTS (SELECT 1
           FROM   [master].[dbo].[sysdatabases]
           WHERE  [name] = N'$(DatabaseName)')
    BEGIN
        ALTER DATABASE [$(DatabaseName)]
            SET READ_COMMITTED_SNAPSHOT OFF;
    END


GO
IF EXISTS (SELECT 1
           FROM   [master].[dbo].[sysdatabases]
           WHERE  [name] = N'$(DatabaseName)')
    BEGIN
        ALTER DATABASE [$(DatabaseName)]
            SET AUTO_UPDATE_STATISTICS_ASYNC OFF,
                PAGE_VERIFY NONE,
                DATE_CORRELATION_OPTIMIZATION OFF,
                DISABLE_BROKER,
                PARAMETERIZATION SIMPLE,
                SUPPLEMENTAL_LOGGING OFF 
            WITH ROLLBACK IMMEDIATE;
    END


GO
IF IS_SRVROLEMEMBER(N'sysadmin') = 1
    BEGIN
        IF EXISTS (SELECT 1
                   FROM   [master].[dbo].[sysdatabases]
                   WHERE  [name] = N'$(DatabaseName)')
            BEGIN
                EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
    SET TRUSTWORTHY OFF,
        DB_CHAINING OFF 
    WITH ROLLBACK IMMEDIATE';
            END
    END
ELSE
    BEGIN
        PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
    END


GO
IF IS_SRVROLEMEMBER(N'sysadmin') = 1
    BEGIN
        IF EXISTS (SELECT 1
                   FROM   [master].[dbo].[sysdatabases]
                   WHERE  [name] = N'$(DatabaseName)')
            BEGIN
                EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
    SET HONOR_BROKER_PRIORITY OFF 
    WITH ROLLBACK IMMEDIATE';
            END
    END
ELSE
    BEGIN
        PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
    END


GO
USE [$(DatabaseName)];


GO
IF fulltextserviceproperty(N'IsFulltextInstalled') = 1
    EXECUTE sp_fulltext_database 'enable';


GO
PRINT N'Creating [dbo].[Table1]...';


GO
CREATE TABLE [dbo].[Table1] (
    [Id]        INT        NOT NULL,
    [namefirst] NCHAR (10) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);


GO
DECLARE @VarDecimalSupported AS BIT;

SELECT @VarDecimalSupported = 0;

IF ((ServerProperty(N'EngineEdition') = 3)
    AND (((@@microsoftversion / power(2, 24) = 9)
          AND (@@microsoftversion & 0xffff >= 3024))
         OR ((@@microsoftversion / power(2, 24) = 10)
             AND (@@microsoftversion & 0xffff >= 1600))))
    SELECT @VarDecimalSupported = 1;

IF (@VarDecimalSupported > 0)
    BEGIN
        EXECUTE sp_db_vardecimal_storage_format N'$(DatabaseName)', 'ON';
    END


GO
PRINT N'Update complete.'
GO

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Snarf0001Commented:
The deployment scripts use a lot of variables, do you have SQLCMD mode set to true?
If so, why is it not running, and what's the message?
0
ADITYA RAOSoftware DeveloperAuthor Commented:
Where  I  have to set  SQLCMD  mode,in SQL  server management  studio  or  Visual Studios SQL server database  project?
0
ADITYA RAOSoftware DeveloperAuthor Commented:
For  YZlat

At  present  I  dont  want  just  script  of  one  table.For  bigger  project  which I  am  likely  to start
I  want  to  learn  how  to use  SQL server database  project?  and  if the  scripts generated  by  this  type  of  project  are usefull.
0
Snarf0001Commented:
From SQL management studio.  I only have 2014 here, but should be the same pretty far back.
One you have the query window open, you should have an item in the main menu for "Query", open that and close to the bottom is a toggle item called "SQLCMD Mode".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ADITYA RAOSoftware DeveloperAuthor Commented:
Ok I  will  try the same
0
ADITYA RAOSoftware DeveloperAuthor Commented:
Yah  It worked  perfectly,but will  you please  able to tell,why  it is  so.We  run  script  from C# . In that  case  what  we  have  to  do?
If  database  is  non exist  we  run  script  to  create database,if it is  exist  we upgrade  it  upto current   version.
So main  thing is  that  now  I  want to  use  sql server database  project  to create  script.What is  this SQLCMD mode  and  what  I  have to  do  when I  want  to  create  database  through  code.
0
Snarf0001Commented:
Unfortunately, you can't enable sqlcmd in .net, it's a management tool.  You'd have to save the query somewhere and run it using sqlcmd.exe or something along those lines, but all of that is probably not necessary.

The main point of sqlcmd in the deployment scripts is for the database name.  When you first run the publish, and it asks for the database name, that gets set in a variable, which is used during all of the creation.

But with a bit of manual modification, you can easily remove most of the variables and run it normally.
If you're generating an alteration script, the only instances are generally "use" statements, which you can just replace with the actual database name you're targeting.

If it's a creation script, several variables are being used, but if you look at the header code, almost all of it is "typically" not necessary.  As long as you have proper permissions to create the db.  All of the exists checks, and permission checks, file paths, options etc... if you REMOVE, and simply call "create database xxx", it will use default file paths and options according to the server.
1
ADITYA RAOSoftware DeveloperAuthor Commented:
I  did  not understood  really  what  you  said.
I  dont  have  much experience  of  deploying  SQL  database  much  I  have  done  it  only  two  projects.
What  we  do  is  create four  scripts  ,One  for  creation of  database.Second  is  creation  of  tables.
Third is  for inserting  in  tables.Fourth  is  upgrade  script  which we  provide  with  every  version.
These  scripts  create  database  if  not  exist  and  update  database.We  are  only  three  people  team.
Anyone  does  database  change  extends upgrade  script  and commits using  svn. And  this  script  we  keep  along exe  while  creating  setup.
Now  I  came  across  SQL  server  Database  project.How  it  can  fit  in  our  scene ? If  we  have  to  include  this  type  of  project  in  our  solution? Will  this  type  of  project  will  reduce  our  work  and  efforts.We  are  in  product  base  company,where  biomedical  instruments  operate  through software  we  write.
0
Snarf0001Commented:
Okay, well what's the scenario?  Is this an application that's distributed to many different clients, each with their own version of the database?
The great thing about the SQL projects, is if YOU have control over everything, the deployment handles scripting whatever is necessary to make changes between target and source.
So keeping track of upgrade scripts is completely unnecessary, as the DB deployment does a live comparison of source and target, and generates the scripts it has to by itself to update.

But that's based on being ABLE to do the live comparison, against a current database.
If this is intended to be a distributed app, then you need to know which version of which database any client has at any given time, which is a different story.

In short, despite how much I love the SQL projects, if this is intended for a distributed app, then it's most likely not what you need.
0
ADITYA RAOSoftware DeveloperAuthor Commented:
Yes  it  is  distributed application with each client having different database.Basically I  am  in  company which manufacture bipomedical  instruments,and I am programmer who develope  application software for the instrument
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.