I'm having a problem in getting a dynamic query in a sp working and the
code is below so can someone please help me? It would be very much
appreciated.
Thanks
Girogio
--------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[Search_Profile]
@Country NVARCHAR(100) = null,
@County NVARCHAR(100) = null,
@Town NVARCHAR(100) = null,
@AType bit,
@PageIndex int,
@NumRows int,
@UsersCount int Output
AS
BEGIN
DECLARE @where_clause NVARCHAR(500);
IF @Country IS NOT NULL
BEGIN
SET @where_clause = @where_clause + ' AND aCountry = "' + @Country +
'"'
END
IF @County IS NOT NULL
BEGIN
SET @where_clause = @where_clause + ' AND aCounty = "' + @County + '"'
END
IF @Town IS NOT NULL
BEGIN
SET @where_clause = @where_clause + ' AND aTown = "' + @Town + '"'
END
IF @AType IS NOT NULL
BEGIN
SET @where_clause = @where_clause + ' AND Independent = "' +
Convert(NVARCHAR, @AType) + '"'
END
DECLARE @Query1 NVARCHAR(1000);
SET @Query1 = 'SELECT @UsersCount=(SELECT COUNT(*) FROM CustomProfile
WHERE aActive = 1 ' + @where_clause
exec(@Query1)
DECLARE @startRowIndex int;
SET @startRowIndex = (@PageIndex * @NumRows) +1;
DECLARE @Query2 NVARCHAR(1000);
SET @Query2 = 'WITH UsersProfiles as (
SELECT ROW_NUMBER() OVER (ORDER BY t.apubName) as Row, t.UserId,
t.apubName, t.aCounty, t.aTown, u.UserName
FROM CustomProfile t, vw_aspnet_Users u
WHERE t.UserID = u.UserID AND aActive = 1 ' + @where_clause
Declare @Query3 NVARCHAR(1000)
SET @Query3 = '
SELECT UserId, apubName, aCounty, aTown, UserName
FROM ' + @Query2 +
' WHERE Row BETWEEN ' + Convert(NVARCHAR, @startRowIndex) + ' AND ' +
Convert(NVARCHAR, @startRowIndex+@NumRows-1)
exec(@Query3)
END 2 6146
(ge*************@hotmail.com) writes: I'm having a problem in getting a dynamic query in a sp working and the code is below so can someone please help me? It would be very much appreciated.
I certainly helps if you are more specific about what your problems
are. The one thing I caught at a glance is that you are using " as a
string delimiter. This is possible if QUOTED_IDENTIFIER is off, but
there is functionality that requires this setting to be on, so don't
do that.
Check out my article about dynamic search conditions on http://www.sommarskog.se/dyn-search.html. There are some examples
that very similar to what you are doing.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
Hi Girogio,
One problem I noticed is that the first time (actually everytime) you try to
set @Where_Clause, It will always set @Where_Clause to Null. Since
@Where_Clause is Null to begin with.
Try This
DECLARE @where_clause NVARCHAR(500);
Set @where_clause = '' -- <New Line
--.....Other Stuff as before
I didn't test this but I hope it helps.
-Dick Christoph
<ge*************@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... I'm having a problem in getting a dynamic query in a sp working and the code is below so can someone please help me? It would be very much appreciated.
Thanks Girogio
--------------------------------------
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go
ALTER PROCEDURE [dbo].[Search_Profile] @Country NVARCHAR(100) = null, @County NVARCHAR(100) = null, @Town NVARCHAR(100) = null, @AType bit, @PageIndex int, @NumRows int, @UsersCount int Output AS BEGIN
DECLARE @where_clause NVARCHAR(500);
IF @Country IS NOT NULL BEGIN SET @where_clause = @where_clause + ' AND aCountry = "' + @Country + '"' END
IF @County IS NOT NULL BEGIN SET @where_clause = @where_clause + ' AND aCounty = "' + @County + '"' END
IF @Town IS NOT NULL BEGIN SET @where_clause = @where_clause + ' AND aTown = "' + @Town + '"' END
IF @AType IS NOT NULL BEGIN SET @where_clause = @where_clause + ' AND Independent = "' + Convert(NVARCHAR, @AType) + '"' END
DECLARE @Query1 NVARCHAR(1000); SET @Query1 = 'SELECT @UsersCount=(SELECT COUNT(*) FROM CustomProfile WHERE aActive = 1 ' + @where_clause exec(@Query1)
DECLARE @startRowIndex int; SET @startRowIndex = (@PageIndex * @NumRows) +1;
DECLARE @Query2 NVARCHAR(1000);
SET @Query2 = 'WITH UsersProfiles as ( SELECT ROW_NUMBER() OVER (ORDER BY t.apubName) as Row, t.UserId, t.apubName, t.aCounty, t.aTown, u.UserName FROM CustomProfile t, vw_aspnet_Users u WHERE t.UserID = u.UserID AND aActive = 1 ' + @where_clause
Declare @Query3 NVARCHAR(1000)
SET @Query3 = ' SELECT UserId, apubName, aCounty, aTown, UserName FROM ' + @Query2 + ' WHERE Row BETWEEN ' + Convert(NVARCHAR, @startRowIndex) + ' AND ' + Convert(NVARCHAR, @startRowIndex+@NumRows-1)
exec(@Query3)
END This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nathan Bloomfield |
last post by:
Does anyone know if there is any documentation which relates to Access2k + ?
or can anyone help adjust the code?
I am having trouble converting the DAO references.
TITLE :INF: How to...
|
by: starace |
last post by:
I have designed a form that has 5 different list boxes where the
selections within each are used as criteria in building a dynamic
query. Some boxes are set for multiple selections but these list...
|
by: WebBuilder451 |
last post by:
I have a series of dynamic link buttons created based upon a datareader. I've
added a click event and it calls the sub ok:
example: "while loop through the reader"
Dim ltrCtrl As New...
|
by: serge |
last post by:
How can I run a single SP by asking multiple sales question either
by using the logical operator AND for all the questions; or using
the logical operator OR for all the questions. So it's always...
|
by: LilC |
last post by:
I'm creating an application that has a standard layout for all pages.
The information that is displayed in the layout will be dynamic based
on the user that is logged in. Thus when a page is...
|
by: Erland Sommarskog |
last post by:
I've uploaded a new version of my article on Dynamic Search Conditions
on http://www.sommarskog.se/dyn-search.html. I've revised the article to
cover SQL 2005, and made a general overhaul of the...
|
by: pukivruki |
last post by:
hi,
I wish to create a temporary table who's name is dynamic based on the
argument.
ALTER PROCEDURE .
@PID1 VARCHAR(50),
@PID2 VARCHAR(50),
@TICKET VARCHAR(20)
|
by: =?Utf-8?B?SmFtZXMgUGFnZQ==?= |
last post by:
I’m trying to create a dynamic asp.net 2.0 siteMapPath control (using VB.net).
Using the xml sitemap I’ve got these three pages:
productGroup.aspx
productListing.aspx
productDetail.aspx
...
|
by: Cindy |
last post by:
I'm trying to use the NEWID function in dynamic SQL and get an error
message Incorrect syntax near the keyword 'ORDER'. Looks like I can't
do an insert with an Order by clause.
Here's the code:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |