473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

error in stored procedure

hi this is my stored procedure in sql server 2005

this stored procedure select data from database (whith search criteria and sort data by @order_by send by caller and default value is sr_id


when i execute this procedure this error will appear "Must declare the scalar variable "@p_result". " and i don't know why please if u can help me




Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[GridView_Sort] (
  2.         @p_version nvarchar(100) = null,                               @p_result nvarchar(100) = null,
  3.         @Salesman int = null ,                               @from_Date datetime = null ,
  4.         @to_date datetime = null,
  5.         @order_by nvarchar(100) = null)
  6.  
  7.  
  8.  
  9. AS
  10. BEGIN
  11. DECLARE @p_build nvarchar(max);
  12.  
  13. if @order_by is null
  14. SET @order_by = ' order BY sr_id';
  15.  
  16.  
  17. SET @p_build = 'SELECT  sr_id AS ID,
  18. sr_version AS Version,
  19. sm_name AS SalesMan_Names,
  20. CASE sr_result WHEN 1 THEN ''OK'' ELSE ''KO'' END  AS Result,
  21. sr_comments AS Comments,
  22. sr_datetime  AS Date 
  23. FROM  SYNC_RESULT ,SALESMAN 
  24. WHERE sm_id = sr_sm_id and
  25.  ((@p_version IS NOT NULL AND sr_version = @p_version) OR (@p_version IS NULL)) 
  26. and ((@p_result IS NOT NULL AND sr_result = @p_result) OR (@p_result IS NULL))
  27.  and ((@Salesman IS NOT NULL AND sm_id = @Salesman) OR (@Salesman IS NULL))
  28.  and ((@from_Date IS NOT NULL AND CONVERT(nvarchar(32),sr_datetime, 112) >= CONVERT(nvarchar(32),@from_Date, 112))  OR (@from_Date IS NULL)) 
  29. and ((@to_date IS NOT NULL AND CONVERT(nvarchar(32),sr_datetime, 112) <= CONVERT(nvarchar(32),@to_date, 112))  OR (@to_date IS NULL))' + @order_by 
  30.  
  31. EXEC sp_executesql @p_build ;
  32.  
  33.  
  34. END
Sep 20 '07 #1
6 2535
Plater
7,872 Expert 4TB
You have a variable called @p_result declared in your stored procedure....it's also used in your query.
You have to assin all variables to use the stored procedure
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[GridView_Sort] (
  2. @p_version nvarchar(100) = null, 
  3. @p_result nvarchar(100) = null,
  4. @Salesman int = null , @from_Date datetime = null ,
  5. @to_date datetime = null,
  6. @order_by nvarchar(100) = null)
  7.  
Sep 20 '07 #2
You have a variable called @p_result declared in your stored procedure....it's also used in your query.
You have to assin all variables to use the stored procedure
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[GridView_Sort] (
  2. @p_version nvarchar(100) = null, 
  3. @p_result nvarchar(100) = null,
  4. @Salesman int = null , @from_Date datetime = null ,
  5. @to_date datetime = null,
  6. @order_by nvarchar(100) = null)
  7.  


how can assin this variables??????????????/
Sep 20 '07 #3
Plater
7,872 Expert 4TB
how can assin this variables??????????????/
The same way you assigned all the others, like @p_version

Are you getting this error when trying to execute the procedure or when you're creating it?
Sep 20 '07 #4
The same way you assigned all the others, like @p_version

Are you getting this error when trying to execute the procedure or when you're creating it?

man the problem occure when i test this stored procedure

but if i execute query without @p_build and EXEC sp_executesql @p_build ; it work fine but it's not the solution coz i have to pass by parameters the order by and i have to create the query like a nvarchar and after execute it by EXEC sp_executesql

so i don't know what have to do

hey in testing i deleted
and ((@p_version IS NOT NULL AND sr_version = @p_version) OR (@p_version IS NULL))

the same error appear but :Must declare the scalar variable "@p_result"
not Must declare the scalar variable "@p_version"

10xxxxxxxxxxxxx man
Sep 20 '07 #5
Frinavale
9,735 Expert Mod 8TB
Hi there!

I've moved this thread into the SQL Server forum.

Good Luck!

-Frinny
Sep 20 '07 #6
rob313
16
The problem is that you have a variable in the dynamic SQL, so the optimizer receives variables but they are not defined to the optimizer, only in you procedure. Change your procedure as follows where you end quote before a variable, then concatenate the variable and then start a new begin quote. I only did the first three variables here, so you will have to do the rest. A good debug technique would be to put a PRINT @p_build statement before your execute so you can see what the dynamic SQL is that you are really trying to execute. There should be no variable names in the final SQL, just the substituted values.

SET @p_build = 'SELECT sr_id AS ID,
sr_version AS Version,
sm_name AS SalesMan_Names,
CASE sr_result WHEN 1 THEN ''OK'' ELSE ''KO'' END AS Result,
sr_comments AS Comments,
sr_datetime AS Date
FROM SYNC_RESULT ,SALESMAN
WHERE sm_id = sr_sm_id and
((' + @p_version + ' IS NOT NULL AND sr_version = '+ @p_version +') OR (' + @p_version + ' IS NULL))
Sep 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: shyner | last post by:
Hi Everyone, I've been battling this for two days with no luck. I'm using SQL Server 2000. Here's the mystery: I've got a stored procedure that takes a single varchar parameter to determine...
1
by: Jen S | last post by:
I feel like I'm missing something obvious here, but I'm stumped... I have a stored procedure with code that looks like: INSERT INTO MyTableA ( ...fields... ) VALUES (...values...) IF...
9
by: dtwilliams | last post by:
OK, i'm trying to do some error checking on stored procedures and am following the advise in Erland Sommarskog's 'Implementing Error Handling with Stored Procedures' document. Can anybody help...
0
by: Rhino | last post by:
I've written several Java stored procedures now (DB2 V7.2) and I'd like to write down a few "best practices" for reference so that I will have them handy for future development. Would the...
1
by: Mihaly | last post by:
I have a stored procedure in SQL Server 2000, and I want to read the error messages from this stored procedure. Please help me for this question: This is the stored procedure. Please supose than...
1
by: Jeremy Ames | last post by:
I have a datagrid that updates the table using a stored procedure. The stored procedure is confirmed to complete correctly, yet the sql data adapter is returning an error that my application is...
4
by: Jack | last post by:
Hi, I am trying to run an example code from a book. However I am getting the following error message: Number: -2147217900 Description: Syntax error or access violation Source: Microsoft OLE...
3
by: Goog79 | last post by:
Hi everyone, first time here, so I'm sorry if this has been covered already ages ago. :( I am trying to learn T-SQL and Stored Procedures and bought the book on these topics by Djan...
4
by: barmatt80 | last post by:
I am stumped on the error reporting with sql server. I was told i need to return @SQLCode(code showing if successful or not) and @ErrMsg(and the message returned). I am clueless on this. I...
1
by: preejith | last post by:
Error Code : 1329, No data - zero rows fetched, selected, or processed. MYSQL I am getting the following error while running a stored procedure in mysql5.0 Error Code : 1329 No data - zero rows...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.