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 -
ALTER PROCEDURE [dbo].[GridView_Sort] (
-
@p_version nvarchar(100) = null, @p_result nvarchar(100) = null,
-
@Salesman int = null , @from_Date datetime = null ,
-
@to_date datetime = null,
-
@order_by nvarchar(100) = null)
-
-
-
-
AS
-
BEGIN
-
DECLARE @p_build nvarchar(max);
-
-
if @order_by is null
-
SET @order_by = ' order BY sr_id';
-
-
-
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))
-
and ((@p_result IS NOT NULL AND sr_result = @p_result) OR (@p_result IS NULL))
-
and ((@Salesman IS NOT NULL AND sm_id = @Salesman) OR (@Salesman IS NULL))
-
and ((@from_Date IS NOT NULL AND CONVERT(nvarchar(32),sr_datetime, 112) >= CONVERT(nvarchar(32),@from_Date, 112)) OR (@from_Date IS NULL))
-
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
-
-
EXEC sp_executesql @p_build ;
-
-
-
END
6 2535
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 -
ALTER PROCEDURE [dbo].[GridView_Sort] (
-
@p_version nvarchar(100) = null,
-
@p_result nvarchar(100) = null,
-
@Salesman int = null , @from_Date datetime = null ,
-
@to_date datetime = null,
-
@order_by nvarchar(100) = null)
-
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 -
ALTER PROCEDURE [dbo].[GridView_Sort] (
-
@p_version nvarchar(100) = null,
-
@p_result nvarchar(100) = null,
-
@Salesman int = null , @from_Date datetime = null ,
-
@to_date datetime = null,
-
@order_by nvarchar(100) = null)
-
how can assin this variables??????????????/
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?
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
Hi there!
I've moved this thread into the SQL Server forum.
Good Luck!
-Frinny
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))
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |