SQL Select Statement Syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HughManity
    New Member
    • Mar 2008
    • 6

    SQL Select Statement Syntax

    I'm getting a " incorrect syntax near ',' " no matter what I try to do. Is there any place where the precise and demanding rules for stringing together SQL commands is explained? For example what do [] signify, and when should it be used? I assume that only a table column name should go between those two characters - is that true? Also, how do you represent an alphanumeric literal? 'abc'? What about a numeric literal - do you put some sort of quotes around it? What about a function - and where is there a list of valid functions representing today's date, the current time, the current user name?

    I'm trying to do an INSERT INTO on a table tblCMActivity with columns defined as following:

    (<ActTypeRef, int,>
    ,<Descr, varchar(30),>
    ,<EntryDate, datetime,>
    ,<EntryTime, datetime,>
    ,<Duration, pDec,>
    ,<Value, pDec,>
    ,<ContactRef, int,>
    ,<CampRef, int,>
    ,<UserId, pUserID,>
    ,<Notes, text,>)

    For each row that the SELECT of r_table yields, I want to insert a row into tblCMActivity, populating the columns as follows:

    ActTypeRef = 1007 (numeric literal)
    Descr = "Imported from Excel" (alpha literal)
    EntryDate = Today's date
    EntryTime = The current time
    Duration = 0 (numeric literal)
    Value = 0 (numeric literal)
    ContactRef = 182 (numeric literal)
    CampRef = 1 (numeric literal)
    UserID = Whoever is using the system
    Notes = NULL

    =============== =========
    My query is the following:

    INSERT INTO [CPU].[dbo].[tblCMActivity]
    ([ActTypeRef]
    ,[Descr]
    ,[EntryDate]
    ,[EntryTime]
    ,[Duration]
    ,[Value]
    ,[ContactRef]
    ,[CampRef]
    ,[UserId]
    ,[Notes])
    SELECT
    (1007,'Imported from Excel',GETDATE( ),GETTIME(),0,0 ,182,1,USER_NAM E(),NULL)

    FROM [CPU].[dbo].[r_table]
    -----------------------------------------------------------
    When I stacked each variable on a separate line, it almost indicated that it especially didn't like the constant "Imported from Excel" - but what on earth does it want? What will satisfy it? This is all taken from examples I've found elsewhere.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by HughManity
    I'm getting a " incorrect syntax near ',' " no matter what I try to do. Is there any place where the precise and demanding rules for stringing together SQL commands is explained? For example what do [] signify, and when should it be used? I assume that only a table column name should go between those two characters - is that true? Also, how do you represent an alphanumeric literal? 'abc'? What about a numeric literal - do you put some sort of quotes around it? What about a function - and where is there a list of valid functions representing today's date, the current time, the current user name?

    I'm trying to do an INSERT INTO on a table tblCMActivity with columns defined as following:

    (<ActTypeRef, int,>
    ,<Descr, varchar(30),>
    ,<EntryDate, datetime,>
    ,<EntryTime, datetime,>
    ,<Duration, pDec,>
    ,<Value, pDec,>
    ,<ContactRef, int,>
    ,<CampRef, int,>
    ,<UserId, pUserID,>
    ,<Notes, text,>)

    For each row that the SELECT of r_table yields, I want to insert a row into tblCMActivity, populating the columns as follows:

    ActTypeRef = 1007 (numeric literal)
    Descr = "Imported from Excel" (alpha literal)
    EntryDate = Today's date
    EntryTime = The current time
    Duration = 0 (numeric literal)
    Value = 0 (numeric literal)
    ContactRef = 182 (numeric literal)
    CampRef = 1 (numeric literal)
    UserID = Whoever is using the system
    Notes = NULL

    =============== =========
    My query is the following:

    INSERT INTO [CPU].[dbo].[tblCMActivity]
    ([ActTypeRef]
    ,[Descr]
    ,[EntryDate]
    ,[EntryTime]
    ,[Duration]
    ,[Value]
    ,[ContactRef]
    ,[CampRef]
    ,[UserId]
    ,[Notes])
    SELECT
    (1007,'Imported from Excel',GETDATE( ),GETTIME(),0,0 ,182,1,USER_NAM E(),NULL)

    FROM [CPU].[dbo].[r_table]
    -----------------------------------------------------------
    When I stacked each variable on a separate line, it almost indicated that it especially didn't like the constant "Imported from Excel" - but what on earth does it want? What will satisfy it? This is all taken from examples I've found elsewhere.
    No need to enclosed the columns in a parenthesis. Here's the complete INSERT INTO syntax.

    -- CK

    Comment

    • HughManity
      New Member
      • Mar 2008
      • 6

      #3
      Originally posted by ck9663
      No need to enclosed the columns in a parenthesis. Here's the complete INSERT INTO syntax.

      -- CK

      Yes, taking out the SELECT parentheses helped greatly.

      Thank you!

      Comment

      Working...