[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Referencing Multiple Tables MSSQL 2000

nvanhaaster

3/27/2007 8:42:00 PM

Hey All,

Just wanted to say i am still green with writing complex SQL Queries
so please bear with me.

I have 2 tables 1 containing an employee number (table:1) which is
bound to the type of system they are using and another table which
specifies the universal employee number(tabel: 2) for that same
person.

I need to be able to reference if there are any records in table 1
that do not have a universal number in table two.

The dilema comes here. Table 2 (universal table) has 4 different
columns of employee numbers. All specific to the system they are
using.

Here is the general layout

Table 1: PK on ID Date Time Location
------------------------------------------------
| ID | Date | Time | Location |
-----------------------------------------------
|0001 | 3/1/07 | 9:50 | 103 |
-----------------------------------------------
|3498 | 3/3/07 | 19:35| 987 |
-----------------------------------------------
|1587 | 3/1/07 | 9:53 | 456 |
-----------------------------------------------


Table 2: PK on UnivID & Location
-------------------------------------------------------------------------------------
| UnivID | Location | ID1 | ID 2 | ID 3 | ID
4 |
-------------------------------------------------------------------------------------
|1030001 | 103 | 0001 | | |
1587 |
--------------------------------------------------------------------------------------
|1030002 | 103 | 0002 | 3498 | 9741 | 1487 |
--------------------------------------------------------------------------------------


What i would like to do is reference table 1 against table 2 to first
make sure that every employee in table 1 has a record in table 2.

I have been looking for days now and come up with the best solution.
Hopefully someone here has a good idea.

I have created this procedure just must be a better way to do this as
I have to display the results in the browser.

ALTER PROCEDURE sp_rnetGet
@Loc VARCHAR(50),
@SDate VARCHAR(10),
@EDate VARCHAR(10)

AS
DECLARE @sql VARCHAR(5000)
DECLARE @ID VARCHAR(50)
DECLARE @location VARCHAR(3)
DECLARE projectCursor CURSOR FOR
SELECT
DISTINCT(employee),
location
FROM
table1
WHERE
location=@Loc AND
callDate BETWEEN @SDate AND @EDate
OPEN projectCursor
FETCH NEXT FROM projectCursor
INTO
@ID,
@location
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = '
IF NOT EXISTS (
(SELECT
univID
FROM
table2
WHERE
rAvayaID='''+@ID+''' OR
xmAvayaID='''+@ID+''' OR
lclID='''+@ID+''' OR
nyID='''+@ID+''' AND
location='''+@location+''')
)
BEGIN
SELECT
DISTINCT(ID,location
FROM
table1
WHERE
ID='''+@ID+''' AND
location='''+@location+''' AND
callDate BETWEEN '''+@SDate+''' AND '''+@EDate+'''
END
'

EXEC(@sql)
FETCH NEXT FROM projectCursor
INTO
@ID,
@location
END
CLOSE projectCursor
DEALLOCATE projectCursor

2 Answers

nvanhaaster

3/27/2007 9:11:00 PM

0

Hello All,

Sorry about the long post as soon as i posted i figured out a solution
to my issue.

>From the results i could see that a regular select statement was not
going to cut it. Instead it must insert the data into a table then
select back all the records.

Thank you for your consideration.

David Portas

3/27/2007 9:53:00 PM

0

On 27 Mar, 22:10, "Nico VanHaaster" <nvanhaas...@caitele.com> wrote:
> Hello All,
>
> Sorry about the long post as soon as i posted i figured out a solution
> to my issue.
>
> >From the results i could see that a regular select statement was not
>
> going to cut it. Instead it must insert the data into a table then
> select back all the records.
>
> Thank you for your consideration.

You did a couple of things wrong. Firstly, don't ever use the sp_
prefix unless you want to create a system procedure in Master. You'll
create problems if you do because sp_ is reserved for system use.

Secondly, don't assume that a cursor is the right solution until
you've exhausted other possibilities. In this case it looks like you
can do the whole thing with one INSERT statement and no cursor or loop
required. (INSERT INTO ... WHERE NOT EXISTS (SELECT ...)).

If you are a novice then you should avoid cursors altogether. Most of
the time there are much better solutions to be had and unfortunately
it takes a lot of experience to judge the right time to use a cursor.

HTH

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/librar...(en-US,SQL.90).aspx
--