| Author |
Topic |
|
vishal_7
Posting Yak Master
127 Posts |
Posted - 2005-09-02 : 16:02:30
|
| Hello, I am trying to create a sp dynamically. However each time, I run my sp, which creates the dynamic SQL for the sp, then it automatically changes:CREATE PROCEDURE ..... toAlter PROCEDURE ....Then I am obvoiusly getting the error msg:Invalid object name. How can I get rid of that? |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-02 : 16:09:41
|
[code]<redFlagAlert> I am trying to create a sp dynamically</redFlagAlert>[/code]How are you generating the CREATE PROC code?EDIT:The real question is why are you dynamically creating SPs. But since it's Friday afternoon before a holiday weekend...I don't really care Be One with the OptimizerTG |
 |
|
|
vishal_7
Posting Yak Master
127 Posts |
Posted - 2005-09-02 : 16:15:12
|
| I am creating the dynamic sp via straight forward dynamic SQL from another sp. The reason why, I create the stored procedure dynamically is that I am creating reports and I need a seperate stored procedures for each of them. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-09-02 : 16:17:43
|
| We need to see the code in order to tell you why it is putting ALTER instead of CREATE.Tara |
 |
|
|
vishal_7
Posting Yak Master
127 Posts |
Posted - 2005-09-02 : 16:20:46
|
quote: Originally posted by tduggan We need to see the code in order to tell you why it is putting ALTER instead of CREATE.Tara
CREATE PROCEDURE [dbo].[pr_CreateSP] @Guid varchar(50), @CreateFields varchar(1000), @InsertFields varchar(1000), @Select varchar(500)ASDECLARE @SQL varchar(4000), @NEWLINE varchar(1)SET @NEWLINE = Char(10)SET @SQL = 'CREATE PROCEDURE dbo.pr_DY' + @guid + ' @Page INT = 1, @RecsPerPage INT = 25, @SortExpression VARCHAR(500) = ''1'', @Query varchar(500) = NULLAS CREATE TABLE #t' + @guid + ' ( [ID] [int] IDENTITY (1, 1) NOT NULL , ' + @CreateFields + ' ) ON [PRIMARY]'SET @SQL = @SQL +'DECLARE @sql AS varchar(2000)SET @sql = ''INSERT INTO #t' + @guid + '(' + @InsertFields + ') ''' + @SelectSET @SQL = @SQL + @NEWLINE + ' IF NOT RTrim(@Query) IS NULL AND LEN(RTrim(@Query)) <> 0 BEGINSET @SQL = @SQL + '' WHERE '' + RTrim(@Query)END'SET @SQL = @SQL+ 'IF NOT RTrim(@SortExpression) IS NULL AND LEN(RTrim(@Query)) <> 0 BEGIN SET @SQL = @SQL + '' Order By '' + RTrim(@SortExpression)END'SET @SQL = @SQL +' EXEC(@SQL)-- Find out the first and last record we wantDECLARE @FirstRec int, @LastRec intSELECT @FirstRec = (@Page - 1) * @RecsPerPageSELECT @LastRec = (@Page * @RecsPerPage + 1)'SET @SQL = @SQL +'-- get total recordsDeclare @TotalCount as intselect @TotalCount = count(ID) from #t' + @guidSET @SQL = @SQL +'-- if the recs per page is 0 then the user wants to see all records. -- usually this is required for printingif @RecsPerPage = 0 BEGINSELECT @LastRec = @TotalCount + 1-- we need to set the recs per page to 25 otherwise we will get an division by 0 error.SET @RecsPerPage = 25End 'SET @SQL = @SQL +'-- number of total pagesDECLARE @TotalPages as intselect @TotalPages = case when @TotalCount % @RecsPerPage = 0 then @TotalCount/@RecsPerPage else @TotalCount/@RecsPerPage + 1End 'SET @SQL = @SQL +'-- Now, return the set of paged records, plus, an indiciation of we-- have more records or not!SELECT *,@TotalPages as TotalPages,MoreRecords = (Select COUNT(ID)FROM #t4' + @guid + ' TIWHERE TI.ID >= @LastRec) FROM #t' + @guid + 'WHERE ID > @FirstRec AND ID < @LastRecDrop Table #t' + @guid + '-- Turn NOCOUNT back OFFSET NOCOUNT OFF'Print(@SQL)EXEC(@SQL)GO |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-02 : 16:21:06
|
| Are you creating the SP dynamically so that everytime you run report instance its using a new SP? If so that is usually handled by input parameters.I have a suspicion...The SP is being created as you intend. Then you are looking at the code by right clicking the SP and selecting "edit". When the code pops up is says "Alter Proc". Is that correct?Be One with the OptimizerTG |
 |
|
|
vishal_7
Posting Yak Master
127 Posts |
Posted - 2005-09-02 : 16:25:09
|
| No, the sp is created only once and assigned to that report. Right now I am just testing it from sql analyzer. It is not recreated. I would be happy if it would create the sp at all, but it doesnt. It replaces create with alter automatically and thinks that the sp already exists, which does not!Thanks |
 |
|
|
vishal_7
Posting Yak Master
127 Posts |
Posted - 2005-09-02 : 16:30:34
|
| Another thing to note is that when I save my sp (pr_CreateSP), then it replaces automatically create to alter. I think it does not happen when I call it, it happens when I saved the sp. I tried to change that back to create and saved it, but when I re-opened it, then it had alter instead of create. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-02 : 16:38:18
|
| what tool are you using to "open up" and "save" the SPs?when you comment out the "exec (@sql)" and just let the "print @sql" run, can you paste the output into a new query analzyer window and run it successfully?Be One with the OptimizerTG |
 |
|
|
vishal_7
Posting Yak Master
127 Posts |
Posted - 2005-09-02 : 16:41:47
|
quote: Originally posted by TG what tool are you using to "open up" and "save" the SPs?when you comment out the "exec (@sql)" and just let the "print @sql" run, can you paste the output into a new query analzyer window and run it successfully?Be One with the OptimizerTG
I am using SQL Enterprise manager to open/save the sp. When I print the SQL and run the printed SQL in another sql anaylzer window, then I get the same error message:Server: Msg 208, Level 16, State 6, Procedure pr_DY46174213209044c4a9146531ec8d76ecb, Line 55Invalid object name 'dbo.pr_DY46174213209044c4a9146531ec8d76ecb'.Its because it replaced the Create to Alter |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-02 : 16:47:40
|
That is weird. I compiled your sp and called it like this:pr_CreateSP @Guid = '46174213209044c4a9146531ec8d76ecb' ,@CreateFields = 'col1 int, col2 int' ,@InsertFields = 'col1, col2' ,@Select = 'select col1, col2 from myTable where 1=2' Everything ran fine. The SP was created: pr_dy46174213209044c4a9146531ec8d76ecbBe One with the OptimizerTG |
 |
|
|
vishal_7
Posting Yak Master
127 Posts |
Posted - 2005-09-02 : 16:49:36
|
| hm....this is really weird. I cant get it working...It changes automatically create to alter... :( |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-02 : 16:57:02
|
| I don't get it. You simply assigned to your @sql varchar(4000) variable a hard-coded value of "CREATE PROC". It can't change to anything other than what you hard coded. The output of your PRINT statement says "Alter"??? Sorry to abandon you, but I have to meet my wife for dinner.Good Luck!!!Be One with the OptimizerTG |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-02 : 22:39:33
|
Ok, back from dinner. Delicious, Calamari with in a marinara sauce.It's gotta be something silly. Check the compiled version of pr_CreateSP, does it match the one you posted (above)?Do all your calls from Query Analyzer (not enterprise managaer) Try the same call I made successfully:pr_CreateSP @Guid = '46174213209044c4a9146531ec8d76ecb' ,@CreateFields = 'col1 int, col2 int' ,@InsertFields = 'col1, col2' ,@Select = 'select col1, col2 from myTable where 1=2' If that works try replacing my parameters with real ones.Be One with the OptimizerTG |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-09-06 : 12:38:52
|
| I have a feeling that this is because he is moving between Query Analyzer and Enterprise Manager. I've seen it say ALTER PROC in Enterprise Manager before due to something a developer did although I don't recall exactly what. It's just that it was saved in syscomments that way, so it'll appear that way.So I'm sure it says CREATE PROC what it does the execute @SQL thing. But from a different viewer, it is switching to ALTER.Tara |
 |
|
|
|