473,851 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help with a SQL Statement - Trying not to use a Cursor

I'm just know basic SQL but not enough to write any complex queries.
The problem I'm facing right now keeps me thinking to use a Cursor but
I've seen a lot of posts on here saying Cursors are bad so I'm hoping
there is a complex query that can give me the data I need.

I have about 6 pages in website where I need to display a datagrid of
information. There should be 5 columns, Filename, and then 4 Category
Titles (These category titles are stored in a table called
PageCategory). I have another table, XREF_Doc_Page that stores the
PageID, DocID (ID to know what file it is), and PageCategoryID. So I
can query this table with a pageID to see all the results that should
be on this page but I don't know how to format it the way I need my
datagrid?

In order to have the records from PageCategory be columns, is this a
crosstab query or something?

My only thoughts right now are to user a cursor to query Pagecategory
and build a temp table somehow with these as the columns?? (Not sure
how'd that would work yet).

So the datagrid would have the 5 columns like I said and then just
list all files associated with this page and put a checkmark under
whichever category it was assigned to (example below...)

Files PageCat1 PageCat2
PageCat3 PageCat4

abc.pdf X
xyz.pdf X
jkl.pdf
x

May 25 '07 #1
4 1941
ky************* @gmail.com wrote:
I'm just know basic SQL but not enough to write any complex queries.
The problem I'm facing right now keeps me thinking to use a Cursor but
I've seen a lot of posts on here saying Cursors are bad so I'm hoping
there is a complex query that can give me the data I need.
First off, post CREATE TABLE statements for your tables. Often a
redesign of the data layout makes the query much simpler.
May 25 '07 #2
Ok here is the PageCategory, Documents, Pages, and XREF_DOC_PAGE
tables

I was setting it up so I could just query the XREF table and pass in
the pageID to give me all the files for that page but as I said before
not sure how to write that query to format it with the columns I want,
Is there a better way to set this up in the database ?
CREATE TABLE [dbo].[PageCategory] (
[PageCategoryID]int IDENTITY(1, 1) NOT NULL,
[PageCategory]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS
NULL,
[LastUpdatedID]int NULL,
[LastUpdateDate]datetime NULL,
PRIMARY KEY CLUSTERED ([PageCategoryID])
)
ON [PRIMARY]
GO

CREATE TABLE [dbo].[Documents] (
[DocID]int IDENTITY(1, 1) NOT NULL,
[DocTypeID]int NULL,
[Title]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[LastUpdateID]int NULL,
[LastUpdateDate]datetime NULL,
[Description]text COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[DocName]text COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED ([DocID])
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO

CREATE TABLE [dbo].[Pages] (
[PageID]int IDENTITY(1, 1) NOT NULL,
[PageShortName]nvarchar(100) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS
NULL,
[PageName]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[PageTitle]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[PageType]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED ([PageID])
)
ON [PRIMARY]
GO

CREATE TABLE [dbo].[XREF_Doc_Page] (
[XREF_Doc_Page_I D]int IDENTITY(1, 1) NOT NULL,
[DocID]int NULL,
[PageID]int NULL,
[PageCategoryID]int NULL,
[SortOrder]int NULL,
[LastUpdateID]int NULL,
[LastUpdateDate]datetime NULL,
PRIMARY KEY CLUSTERED ([XREF_Doc_Page_I D])
)
ON [PRIMARY]
GO

May 25 '07 #3
On May 25, 11:50 pm, kyle.fitzger... @gmail.com wrote:
Ok here is the PageCategory, Documents, Pages, and XREF_DOC_PAGE
tables

I was setting it up so I could just query the XREF table and pass in
the pageID to give me all the files for that page but as I said before
not sure how to write that query to format it with the columns I want,
Is there a better way to set this up in the database ?

CREATE TABLE [dbo].[PageCategory] (
[PageCategoryID]int IDENTITY(1, 1) NOT NULL,
[PageCategory]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS
NULL,
[LastUpdatedID]int NULL,
[LastUpdateDate]datetime NULL,
PRIMARY KEY CLUSTERED ([PageCategoryID])
)
ON [PRIMARY]
GO

CREATE TABLE [dbo].[Documents] (
[DocID]int IDENTITY(1, 1) NOT NULL,
[DocTypeID]int NULL,
[Title]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[LastUpdateID]int NULL,
[LastUpdateDate]datetime NULL,
[Description]text COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[DocName]text COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED ([DocID])
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO

CREATE TABLE [dbo].[Pages] (
[PageID]int IDENTITY(1, 1) NOT NULL,
[PageShortName]nvarchar(100) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS
NULL,
[PageName]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[PageTitle]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
[PageType]nvarchar(250) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED ([PageID])
)
ON [PRIMARY]
GO

CREATE TABLE [dbo].[XREF_Doc_Page] (
[XREF_Doc_Page_I D]int IDENTITY(1, 1) NOT NULL,
[DocID]int NULL,
[PageID]int NULL,
[PageCategoryID]int NULL,
[SortOrder]int NULL,
[LastUpdateID]int NULL,
[LastUpdateDate]datetime NULL,
PRIMARY KEY CLUSTERED ([XREF_Doc_Page_I D])
)
ON [PRIMARY]
GO

You need a dynamic cross tab query , If Pagecategory are finite and
constant , you can do some thing like this
Select b.docname,
MAX(case when PageCategoryID = 1 then c.pagecategory end ) as
Pagecategory01,
MAX(case when PageCategoryID = 2 then c.pagecategory end ) as
Pagecategory02,
MAX(case when PageCategoryID = 3 then c.pagecategory end ) as
Pagecategory03,
MAX(case when PageCategoryID = 4 then c.pagecategory end ) as
Pagecategory04
FROM
(select distinct docid,pagecateg oryid from XREF_Doc_Page) a
inner join documents b where a.docid = b.docid
inner join pagecatefory c where a.pagecategoryi d = c.pagecategoryi d
group by b.docname
order by b.docname
May 26 '07 #4
hi :

The following is a sample of how to change a cursor query into a non-
cursor query
Trust this helps
-RS

declare
@where varchar(10),
@when varchar(7),
@who varchar(5),
--@continue int,
@cp_id int,
@count int,
@loop_ctr int,
@loop_max int

--declare c1 cursor
--local
--for
insert into temp_www
([when],[where],who)
(
select distinct
[when],
[where],
who
from cicaprod_duplic ates
)
set @loop_max = (select max(www_id) from temp_www)
set @loop_ctr = 1
--open c1
print 'Start loop...'
while(@loop_ctr <= @loop_max)
begin
select @when = [when], @where = [where, @who = who
from temp_www
where www_id = @loop_ctr

print @when+' '+@where+' '+@who

set @count = (select count(cp_id) from cicaprod_nodup
where [when] = @when and [where] = @where and who = @who)

print '@count = '+cast(@count as varchar(5))

set @loop_ctr = @loop_ctr + 1
End -- while

print 'End loop...'
--close c1
--deallocate c1

May 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4380
by: Jason Callas | last post by:
I have a stored procedure that runs as a step in a scheduled job. For some reason the job does not seem to finish when ran from the job but does fine when run from a window in SQL Query. I know the job is not working because the number of rows that are inserted into the table (see code) is considerably less than the manual runnning of it. I have included the code for the stored procedure, the output from the job, and the output from...
2
8678
by: Amy | last post by:
This is what I want to do: 1. Delete all tables in database with table names that ends with a number. 2. Leave all other tables in tact. 3. Table names are unknown. 4. Numbers attached to table names are unknown. 5. Unknown number of tables in database. For example:
2
4005
by: olekristianvillabo | last post by:
The method cursor.executemany is there in order to avoid multiple calls to cursor.execute(). I have tried, with success, to do like every single example (that I have found on the www) on the subject shows, to use a insert statement on the form: statement = INSERT INTO table (colA,colB,colC) values (%s,%s,%s) and pass in a list containing tuples list =
3
2511
by: David Mitchell | last post by:
Hello, I am a complete beginner with Python. I've managed to get mod_python up and running with Apache2 and I'm trying to a simple insert into a table in a MySQL database. I'm using the MySQLdb library for connectivity. I can read from the database no problem, but when I do an insert, the value never gets added to the database, even though there is no error, and the SQL is fine (I print out the SQL statement in the function). When I...
1
4246
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here. From the log it looks like the problem happens when 2 threads insert 1 record each in the same table and then try to aquire a NS (Next Key Share) lock on the record inserterd by the other thread. Thanks Rohit
2
2590
by: Rhino | last post by:
Is there some kind of requirement that a DECLARE CONTINUE HANDLER statement follow the DECLARE CURSOR statement in an SQL stored procedure? I am running DB2 V8.2.1 on Windows XP and am writing my first few SQL stored procedures. Given this fragment (there is more code following the last statement): ---------------------------------------------------------------------------- -----------------------------------
2
1560
by: Alex | last post by:
In the code below, clicking on the button ButtonChangeCursor changes the form's cursor to a WaitCursor. Clicking the button ButtonRestoreCursor changes the form's cursor back to its original cursor. For the reasons I explain below I wouldn't expect this code to behave as it does, so why does it work? Public Class Form1 Dim OldCursor As Cursor Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
2
3418
by: shorti | last post by:
Can you not use the 'FOR READ ONLY' option with the cursor declare unless you put the SELECT statement in the declare? re: EXEC SQL DECLARE c2 CURSOR FOR s2 FOR READ ONLY; errors with:
2
8182
by: kya2 | last post by:
I am not able to create following store procedure. CREATE PROCEDURE DBSAMBA.InsertDeleteBatch(OUT norows INT ) RESULT SETS 1 LANGUAGE SQL BEGIN part1 DECLARE TOTAL_LEFT INT DEFAULT 0; SELECT COUNT(*)INTO TOTAL_LEFT FROM DBSAMBA.REPORTEDTRANSACTION_S; WHILE (TOTAL_LEFT > 0) DO
0
9747
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10728
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9506
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7906
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5933
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4549
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.