CREATE statement

  • +1

    Nice Question ....

  • GOOD QUESTION...

    NICE EXPLANATION FROM GUYS lOKESH, DANNY... 😛

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Nice question... I really learned two points.... 1. with go- how to create stored procedures and functions

    2. without go- how to create stroed procedures and functions........:-D

    Manik
    You cannot get to the top by sitting on your bottom.

  • Good Question.... lots of fun to read through... I started saying Dibbiddy Dibbidy Dibbiddy....



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • Nothing wrong with the following for step 6 though (and the equivalent for step 4):

    USE DBDB;

    -- no 'GO' necessary

    IF OBJECT_ID('dbo.udfUDF') IS NULL

    exec('CREATE FUNCTION dbo.udfUDF(@f int) RETURNS INT AS BEGIN SET @f=1 RETURN @f END;');

    In my opinion 'GO' is very overused. It's almost "stick a go in everywhere just in case", particularly in code examples.

    For instance in this QOD, all except one 'GO' can be ommitted and the whole script executed in one 'GO' (but two batches(!)):

    -- Query #1

    USE master;

    IF DB_ID('DBDB') IS NOT NULL DROP DATABASE DBDB

    -- Query #2

    IF DB_ID('DBDB') IS NULL CREATE DATABASE DBDB

    go -- this one is necessary otherwise the rest of the script won't compile.

    -- Query #3

    USE DBDB;

    IF OBJECT_ID('dbo.TBTB') IS NULL CREATE Table dbo.TBTB (tb1 int)

    -- Query #4

    IF OBJECT_ID('dbo.uspUSP') IS NULL

    exec('CREATE PROCEDURE dbo.uspUSP AS SET NOCOUNT OFF');

    -- Query #5

    IF OBJECT_ID('dbo.TYTP') IS NULL CREATE TYPE dbo.TPTP AS TABLE(TYTB int)

    -- Query #6

    IF OBJECT_ID('dbo.udfUDF') IS NULL

    exec('CREATE FUNCTION dbo.udfUDF(@f int) RETURNS INT AS BEGIN SET @f=1 RETURN @f END;');

  • I ran all 6 in my SQL Server 2008R2 and the only one that did not error was the first one. Query 2 gives me error:

    Msg 5133, Level 16, State 1, Line 1

    Directory lookup for the file "D:\Database files\SQL Server\DBDB.mdf" failed with the operating system error 3(The system cannot find the path specified.).

    and this caused every subsequent query to fail. I even changed the query 2 to use DB_ID. I changed back to original which I knew would fail and yes, that fails too.

    So what is going on here? I am running SQL Server 2008R2 Express. Could that be it? I am just learning SQL Server. I work mostly with Oracle.

  • Thanks for the question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • I am nominating this for the Most Misguided QotD of the Year Award.

  • Revenant (3/7/2013)


    I am nominating this for the Most Misguided QotD of the Year Award.

    😀

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • sgehret 2557 (3/7/2013)


    I ran all 6 in my SQL Server 2008R2 and the only one that did not error was the first one. Query 2 gives me error:

    Msg 5133, Level 16, State 1, Line 1

    Directory lookup for the file "D:\Database files\SQL Server\DBDB.mdf" failed with the operating system error 3(The system cannot find the path specified.).

    and this caused every subsequent query to fail. I even changed the query 2 to use DB_ID. I changed back to original which I knew would fail and yes, that fails too.

    So what is going on here? I am running SQL Server 2008R2 Express. Could that be it? I am just learning SQL Server. I work mostly with Oracle.

    sgehret: If the directory doesn't exist, create it. If the directory exists (and there's enough free space on the disk for a new database), start services.msc and see what user the SQL Server service is running under, then look at the permissions on the directory to check the service user has full access.

  • Revenant (3/7/2013)


    I am nominating this for the Most Misguided QotD of the Year Award.

    I think it's a poor fifth in that race. 😎

    Tom

  • Interesting question, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Interesting question, thanks so much!

  • L' Eomot Inversé (3/8/2013)


    Revenant (3/7/2013)


    I am nominating this for the Most Misguided QotD of the Year Award.

    I think it's a poor fifth in that race. 😎

    Tom,

    This being early March, your comment says volumes about the QotD so far this year.

    The thing I must say about all those questions is that each has given the community a great platform for discussion about topics and uses that might not have taken place without the poor questions.

    M.

    Not all gray hairs are Dinosaurs!

  • Hi,

    It's official. I'm an idiot. I have my local SQL Server setup to create data files on my D drive due to space issues. But I connected to a database on another server to test this question. Big duh! on my part. When I finally figured it out and ran it on my Server and saw the correct results.

    Sorry for any inconvenience. Thanks for your information.

    Take care,

    Scott

Viewing 15 posts - 16 through 30 (of 41 total)

You must be logged in to reply to this topic. Login to reply