Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Execute String sql

Execute String sql

Scheduled Pinned Locked Moved Database
sharepointdatabasesql-serversysadminhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SVb net
    wrote on last edited by
    #1

    IF EXISTS (SELECT name FROM sysobjects WHERE name = N'Bs_Proc_Serial' AND type = 'P') Drop Proc Bs_Proc_Serial GO Create Proc Bs_Proc_Serial @TableName Char(25) , @ColumnSearch Char(25) AS /* G e t S e r i a l I d D y n a m i c */ Declare @Ex NVarChar(500) Declare @Par NVarChar(100) Set @Par= N'@Serial bigint' Set @Ex = N'Select @Serial = Count(*) From ' + @TableName Set @Ex = @Ex + N' while (@Serial < 100000000 )' Set @Ex = @Ex + N' begin ' Set @Ex = @Ex + N' if Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= RTRIM(@Serial)) ' Set @Ex = @Ex + N' begin ' Set @Ex = @Ex + N' If (@serial < 10 ) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' If (@Serial < 100 ) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' If (@Serial < 1000) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' End ' Set @Ex = @Ex + N' Set @Serial = @Serial + 1 ' Set @Ex = @Ex + N' End ' Set @Ex = @Ex + N' if (@serial < 10 ) ' Set @Ex = @Ex + N' Select ''00'' + Rtrim(@Serial) ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' if (@Serial < 100 ) ' Set @Ex = @Ex + N' Select ''0'' + Rtrim(@Serial) ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' Select Rtrim(@Serial) ' -- Set @Ex = N'Select Count(*) From ' -- Set @Ex = @Ex + @TableName Execute sp_executesql @Ex,@Par Go Exec Bs_Proc_Serial 'Ic_Category' ,'CategoryId' sql server msg: Server: Msg 170, Level 15, State 1, Line 1 Line 1: Incorrect syntax near '@Serial'. what's problem?

    123

    A J 2 Replies Last reply
    0
    • S SVb net

      IF EXISTS (SELECT name FROM sysobjects WHERE name = N'Bs_Proc_Serial' AND type = 'P') Drop Proc Bs_Proc_Serial GO Create Proc Bs_Proc_Serial @TableName Char(25) , @ColumnSearch Char(25) AS /* G e t S e r i a l I d D y n a m i c */ Declare @Ex NVarChar(500) Declare @Par NVarChar(100) Set @Par= N'@Serial bigint' Set @Ex = N'Select @Serial = Count(*) From ' + @TableName Set @Ex = @Ex + N' while (@Serial < 100000000 )' Set @Ex = @Ex + N' begin ' Set @Ex = @Ex + N' if Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= RTRIM(@Serial)) ' Set @Ex = @Ex + N' begin ' Set @Ex = @Ex + N' If (@serial < 10 ) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' If (@Serial < 100 ) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' If (@Serial < 1000) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' End ' Set @Ex = @Ex + N' Set @Serial = @Serial + 1 ' Set @Ex = @Ex + N' End ' Set @Ex = @Ex + N' if (@serial < 10 ) ' Set @Ex = @Ex + N' Select ''00'' + Rtrim(@Serial) ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' if (@Serial < 100 ) ' Set @Ex = @Ex + N' Select ''0'' + Rtrim(@Serial) ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' Select Rtrim(@Serial) ' -- Set @Ex = N'Select Count(*) From ' -- Set @Ex = @Ex + @TableName Execute sp_executesql @Ex,@Par Go Exec Bs_Proc_Serial 'Ic_Category' ,'CategoryId' sql server msg: Server: Msg 170, Level 15, State 1, Line 1 Line 1: Incorrect syntax near '@Serial'. what's problem?

      123

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      Try adding: PRINT @Ex so that you can be sure of the SQL string that you are building up. Regards Andy

      1 Reply Last reply
      0
      • S SVb net

        IF EXISTS (SELECT name FROM sysobjects WHERE name = N'Bs_Proc_Serial' AND type = 'P') Drop Proc Bs_Proc_Serial GO Create Proc Bs_Proc_Serial @TableName Char(25) , @ColumnSearch Char(25) AS /* G e t S e r i a l I d D y n a m i c */ Declare @Ex NVarChar(500) Declare @Par NVarChar(100) Set @Par= N'@Serial bigint' Set @Ex = N'Select @Serial = Count(*) From ' + @TableName Set @Ex = @Ex + N' while (@Serial < 100000000 )' Set @Ex = @Ex + N' begin ' Set @Ex = @Ex + N' if Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= RTRIM(@Serial)) ' Set @Ex = @Ex + N' begin ' Set @Ex = @Ex + N' If (@serial < 10 ) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' If (@Serial < 100 ) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' If (@Serial < 1000) ' Set @Ex = @Ex + N' If Not Exists(Select * From ' + @TableName + N'Where ' + @ColumnSearch + N'= ''00''+ RTRIM(@Serial)) ' Set @Ex = @Ex + N' Break ' Set @Ex = @Ex + N' End ' Set @Ex = @Ex + N' Set @Serial = @Serial + 1 ' Set @Ex = @Ex + N' End ' Set @Ex = @Ex + N' if (@serial < 10 ) ' Set @Ex = @Ex + N' Select ''00'' + Rtrim(@Serial) ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' if (@Serial < 100 ) ' Set @Ex = @Ex + N' Select ''0'' + Rtrim(@Serial) ' Set @Ex = @Ex + N' Else ' Set @Ex = @Ex + N' Select Rtrim(@Serial) ' -- Set @Ex = N'Select Count(*) From ' -- Set @Ex = @Ex + @TableName Execute sp_executesql @Ex,@Par Go Exec Bs_Proc_Serial 'Ic_Category' ,'CategoryId' sql server msg: Server: Msg 170, Level 15, State 1, Line 1 Line 1: Incorrect syntax near '@Serial'. what's problem?

        123

        J Offline
        J Offline
        JM_G
        wrote on last edited by
        #3

        I think you should launch sp_executesql by providing a value for @serial Ex : Execute sp_executesql @Ex,@Par,@serial=0 But I don't get the point of your SP, maybe should @serial be an OUTPUT param ?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups