Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple select Statement

Author  Topic 

kopzen
Starting Member

34 Posts

Posted - 2010-04-12 : 04:59:27
Hi Good People

I am runnung this quirie on SQL2005:

SELECT UserDIDNumber From dbo.Users

which returns phone extensions from 2000 - 4000.

Now I want to update this quirie to return available extensions .

Please help

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 05:08:18
What do you define as "Available Extensions"?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

kopzen
Starting Member

34 Posts

Posted - 2010-04-12 : 05:12:45
I mean telephone extensions listed on the database, see everytime theres a new user I have to physically go the records trying to find a spare number to use. So I need to automate that process so that SQL can find me the numbers for me instead.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-12 : 05:20:38
Please could you provide
table structure
sample data and
wanted output in relation to sample data?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 05:29:52
quote:
Originally posted by kopzen

I mean telephone extensions listed on the database, see everytime theres a new user I have to physically go the records trying to find a spare number to use. So I need to automate that process so that SQL can find me the numbers for me instead.



So what you want is, a list of Extensions that are not currently being used? Do you have a list of all possible extensions?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

kopzen
Starting Member

34 Posts

Posted - 2010-04-12 : 05:45:45
Yes, I want a list of free extensions.
EG.
the UserDIDNumber column contains:
3000
3001
3002
3004
3005
3007

So I want to design a statement to return numbers that not used beteen 2000 and 4000:
EG.
3003
3006 as results

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-12 : 05:48:12
Do you mean that the allowed list of phone extensions is between the numbers 2000 and 4000? so there are 2000 possible phone extensions and you want sql to show you which are not in use?

If you don't have a tally table (table of numbers) set up then you can make use of a tally table function to do this:

MVJ wrote one a while ago:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685

and then you'd do something like:


select n.[number]
FROM
dbo.F_TABLE_NUMBER_RANGE(2000,4000) AS n
WHERE
NOT EXISTS (SELECT 1 FROM users u WHERE u.[UserDIDNumber] = n.[number])



IF you didn't want to use the function then you could hard code it as a CTE:
; WITH powers AS (
SELECT
[N01], [N02], [N03]
FROM
(
SELECT [N01] = 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11
UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15
) NO1

CROSS JOIN (
SELECT [N02] = 0 UNION SELECT 16 UNION SELECT 32 UNION SELECT 48
UNION SELECT 64 UNION SELECT 80 UNION SELECT 96 UNION SELECT 112
UNION SELECT 128 UNION SELECT 144 UNION SELECT 160 UNION SELECT 176
UNION SELECT 192 UNION SELECT 208 UNION SELECT 224 UNION SELECT 240
) NO2

CROSS JOIN (
SELECT [N03] = 0 UNION SELECT 256 UNION SELECT 512 UNION SELECT 768
UNION SELECT 1024 UNION SELECT 1280 UNION SELECT 1536 UNION SELECT 1792
UNION SELECT 2048 UNION SELECT 2304 UNION SELECT 2560 UNION SELECT 2816
UNION SELECT 3072 UNION SELECT 3328 UNION SELECT 3584 UNION SELECT 3840
) N03
)

, NUMBER_RANGE AS (

SELECT TOP 100 PERCENT
[number] = ( a.[number] + b.[number] ) + 2000
FROM
(
SELECT TOP 100 PERCENT
[number] = CAST([N01] + [N02] + [N03] AS INT)
FROM
powers
WHERE
[N01] + [N02] + [N03] < CAST(CEILING(SQRT(ABS(2001))) AS INT)
ORDER BY
1
) a
CROSS JOIN (
SELECT TOP 100 PERCENT
[number] = CAST(([N01] + [N02] + [N03]) * CAST(CEILING(SQRT(ABS(2001 ))) AS INT) AS INT)
FROM
powers
WHERE
[N01] + [N02] + [N03] < CAST(CEILING(SQRT(ABS(2001 ))) AS INT)
ORDER BY
1
) b
WHERE
a.[number] + b.[number] < ABS(2001)
AND (16777216 - ABS(1999)) > 0
)

select n.[number]
FROM
number_range n
WHERE
NOT EXISTS (SELECT 1 FROM users u WHERE UserDIDNumber = [number])

But the function is very usefull

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-12 : 05:53:04
Wouldn't a simple not between clause be useful in this case?

PBUH
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-12 : 06:00:30
quote:

Wouldn't a simple not between clause be useful in this case?

PBUH
Edited by - Idera on 04/12/2010 05:53:31


How would that bring back a number that wasn't in use? You could find users that didn't have an extension between 2000 and 4000 but that wouldn't tell you which extension numbers were available....


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 06:05:55
quote:
Originally posted by Idera

Wouldn't a simple not between clause be useful in this case?

PBUH



Not if you're trying to select numbers that don't exist in any table.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

kopzen
Starting Member

34 Posts

Posted - 2010-04-12 : 06:19:30
quote:
Originally posted by Transact Charlie

Do you mean that the allowed list of phone extensions is between the numbers 2000 and 4000? so there are 2000 possible phone extensions and you want sql to show you which are not in use?
[/red]



You are 100% correct, thats exately what Im trying to do. when I run the function I get:


Msg 208, Level 16, Line 1
Invalid object name 'dbo.F_TABLE_NUMBER_RANGE'

So basically I need the numbers that are not in the database but in the specified range.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-12 : 06:27:13
Well -- did you actually go and get the function? If you follow the link I posted you'd be able to grab a version. Here's the one I used.:

/****** Object: UserDefinedFunction [dbo].[F_TABLE_NUMBER_RANGE] Script Date: 04/12/2010 11:26:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[F_TABLE_NUMBER_RANGE]
(
@startNumber INT
, @endNumber INT
)
RETURNS TABLE AS RETURN
(
WITH powers AS (
SELECT
[N01], [N02], [N03]
FROM
(
SELECT [N01] = 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11
UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15
) NO1

CROSS JOIN (
SELECT [N02] = 0 UNION SELECT 16 UNION SELECT 32 UNION SELECT 48
UNION SELECT 64 UNION SELECT 80 UNION SELECT 96 UNION SELECT 112
UNION SELECT 128 UNION SELECT 144 UNION SELECT 160 UNION SELECT 176
UNION SELECT 192 UNION SELECT 208 UNION SELECT 224 UNION SELECT 240
) NO2

CROSS JOIN (
SELECT [N03] = 0 UNION SELECT 256 UNION SELECT 512 UNION SELECT 768
UNION SELECT 1024 UNION SELECT 1280 UNION SELECT 1536 UNION SELECT 1792
UNION SELECT 2048 UNION SELECT 2304 UNION SELECT 2560 UNION SELECT 2816
UNION SELECT 3072 UNION SELECT 3328 UNION SELECT 3584 UNION SELECT 3840
) N03
)
SELECT TOP 100 PERCENT
[number] = ( a.[number] + b.[number] ) + CASE WHEN @startNumber <= @endNumber THEN @startNumber ELSE @endNumber END
FROM
(
SELECT TOP 100 PERCENT
[number] = CAST([N01] + [N02] + [N03] AS INT)
FROM
powers
WHERE
[N01] + [N02] + [N03] < CAST(CEILING(SQRT(ABS(@startNumber - @endNumber) + 1 )) AS INT)
ORDER BY
1
) a
CROSS JOIN (
SELECT TOP 100 PERCENT
[number] = CAST(([N01] + [N02] + [N03]) * CAST(CEILING(SQRT(ABS(@startNumber - @endNumber) + 1 )) AS INT) AS INT)
FROM
powers
WHERE
[N01] + [N02] + [N03] < CAST(CEILING(SQRT(ABS(@startNumber - @endNumber) + 1 )) AS INT)
ORDER BY
1
) b
WHERE
a.[number] + b.[number] < ABS(@startNumber - @endNumber) + 1
AND (16777216 - ABS( @startNumber - @endNumber ) - 1 ) > 0
ORDER BY
1
)






Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 06:29:10
You need to add the function 'dbo.F_TABLE_NUMBER_RANGE' from the specified link to your database. If that's not an option, then run the longer query, which will do the same thing.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-04-12 : 06:49:51
Can this work

DECLARE @i AS INT
DECLARE @temptable AS TABLE( ext int)

SELECT @i = 2000
WHILE @i <= 4000
BEGIN
IF NOT EXISTS ( SELECT 1 FROM table1 WHERE Ext = @i )
BEGIN
INSERT INTO @temptable VALUES(@i)
END
SELECT @i = @i + 1
END
SELECT * FROM @temptable


Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 06:55:28
quote:
Originally posted by vaibhavktiwari83

Can this work


Yes, but it'd be much slower than the query posted by charlie.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-12 : 07:03:48
For anyone looking at this thread. The point you should be taking away is that a table of numbers can be very useful. Not just in this specific scenario but for many, many things. you should consider making a static tally table (with as many numbers in it as you feel you'd need).

The tally table function MVJ wrote is very fast but it sill isn't as quick as a properly indexed tally table. The same can be said for a table of dates (which is possibly even more useful).

vaibhavktiwari83 - your solution will work and as DBA in the making pointed out it will be slow. Consider that you are performing 2000 select statements. and possibly doing a table scan for each one (depending on if there is a nice index on the target column or not).


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

kopzen
Starting Member

34 Posts

Posted - 2010-04-12 : 07:48:02
Sorry Guys still not working, I Right clicked/Script table as/ Update to so you can get the table structure. I never thought this would be so hard to do.

UPDATE [RightFax].[dbo].[Users]
SET [GroupID] = <GroupID, int,>
,[UserFlags] = <UserFlags, bigint,>
,[UserID] = <UserID, varchar(22),>
,[UserName] = <UserName, varchar(29),>
,[Password] = <Password, varchar(34),>
,[UserDIDNumber] = <UserDIDNumber, bigint,>
,[FCSModelName] = <FCSModelName, varchar(14),>
,[NotifyType] = <NotifyType, int,>
,[STDIDNum] = <STDIDNum, varchar(32),>
,[STGenFaxNum] = <STGenFaxNum, varchar(32),>
,[STCallback] = <STCallback, smallint,>
,[STOperNum] = <STOperNum, varchar(32),>
,[STFromName] = <STFromName, varchar(60),>
,[STFromPhone] = <STFromPhone, varchar(32),>
,[STToFaxNum] = <STToFaxNum, varchar(32),>
,[STToContactNum] = <STToContactNum, varchar(32),>
,[STToName] = <STToName, varchar(60),>
,[STToCompany] = <STToCompany, varchar(60),>
,[STToCityState] = <STToCityState, varchar(60),>
,[AutoPrintNetPrintID] = <AutoPrintNetPrintID, varchar(10),>
,[DefaultPrinter] = <DefaultPrinter, varchar(10),>
,[WebImageFormat] = <WebImageFormat, smallint,>
,[LastUpgradedFolderID] = <LastUpgradedFolderID, smallint,>
,[AutoSentPrinter] = <AutoSentPrinter, varchar(10),>
,[AutoPrintFlags] = <AutoPrintFlags, int,>
,[SubscriberID] = <SubscriberID, bigint,>
,[UserFlags2] = <UserFlags2, bigint,>
,[HighestPriority] = <HighestPriority, smallint,>
,[DefaultPriority] = <DefaultPriority, smallint,>
,[NewDocNotifyUser] = <NewDocNotifyUser, varchar(22),>
,[RouteType] = <RouteType, smallint,>
,[RouteFmt] = <RouteFmt, smallint,>
,[AFPhone] = <AFPhone, varchar(32),>
,[SendNotify] = <SendNotify, int,>
,[ReceiveNotify] = <ReceiveNotify, int,>
,[UpdateInterval] = <UpdateInterval, int,>
,[AutoDelete] = <AutoDelete, int,>
,[DefaultViewScale] = <DefaultViewScale, int,>
,[AutoOCRFlags] = <AutoOCRFlags, int,>
,[AutoOCRLayout] = <AutoOCRLayout, smallint,>
,[AutoOCRFormat] = <AutoOCRFormat, smallint,>
,[AutoOCRExt] = <AutoOCRExt, varchar(4),>
,[AFUser] = <AFUser, smallint,>
,[RouteDeleteAfter] = <RouteDeleteAfter, smallint,>
,[ChangeTag] = <ChangeTag, bigint,>
,[RouteInfo] = <RouteInfo, varchar(100),>
,[NWDSName] = <NWDSName, varchar(80),>
,[Folders] = <Folders, varchar(256),>
,[OtherPBID_0] = <OtherPBID_0, varchar(22),>
,[OtherPBID_1] = <OtherPBID_1, varchar(22),>
,[OtherPBID_2] = <OtherPBID_2, varchar(22),>
,[OtherPBID_3] = <OtherPBID_3, varchar(22),>
,[OtherPBPass_0] = <OtherPBPass_0, varchar(34),>
,[OtherPBPass_1] = <OtherPBPass_1, varchar(34),>
,[OtherPBPass_2] = <OtherPBPass_2, varchar(34),>
,[OtherPBPass_3] = <OtherPBPass_3, varchar(34),>
,[RPOStartPage] = <RPOStartPage, smallint,>
,[RPOEndPage] = <RPOEndPage, smallint,>
,[RPOFlags] = <RPOFlags, smallint,>
,[RPORes] = <RPORes, smallint,>
,[RPOSize] = <RPOSize, smallint,>
,[RPOSource] = <RPOSource, smallint,>
,[RPOOutputBin] = <RPOOutputBin, smallint,>
,[RPODuplex] = <RPODuplex, smallint,>
,[RPOSecurityCode] = <RPOSecurityCode, int,>
,[RPOCopies] = <RPOCopies, smallint,>
,[EmailRouteForm] = <EmailRouteForm, smallint,>
,[RPOAcctCode] = <RPOAcctCode, binary(8),>
,[RPOPriority] = <RPOPriority, smallint,>
,[RPOOther] = <RPOOther, smallint,>
,[DefaultBI1] = <DefaultBI1, varchar(16),>
,[DefaultBI2] = <DefaultBI2, varchar(16),>
,[SID] = <SID, varchar(88),>
,[EmailAddr] = <EmailAddr, varchar(256),>
,[SMSServiceID] = <SMSServiceID, varchar(22),>
,[MobileAddr] = <MobileAddr, varchar(32),>
,[NotifyInfo] = <NotifyInfo, varchar(100),>
,[ObjectGuid] = <ObjectGuid, uniqueidentifier,>
,[UsnCreated] = <UsnCreated, bigint,>
,[UsnUpdated] = <UsnUpdated, bigint,>
,[SyncGuid] = <SyncGuid, varchar(128),>
,[SyncSource] = <SyncSource, varchar(32),>
,[SyncUSN] = <SyncUSN, bigint,>
WHERE <Search Conditions,,>



Inside column UserDIDNumber there are tel extensions. I am just learning SQL so please explain to me like you are talking to a 6 year old.

thanks
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 07:56:59
Kopzen, TC posted a script to create function that produces a list of numbers, and a query that calls that function. To use it, you have to first create the function by running the create script. Make sure you create the function in the same database as you're selecting the data from.

Also, to script a table structure, Right clicked/Script table as/CREATE, rather than update.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-12 : 08:00:54
If things aren't working please post what you've tried from the suggestions and post the error messages you get.

Are you using SQL SERVER 2005 or higher?

If you are using SQL SERVER 2000 you'd have to use an earlier version of the function.

Don't worry -- it just *seems* hard. In reality this problem is fairly simple but there a lot of new things to learn if you haven't got into more advanced SQL like functions or common table expressions. just post what you've tried and we'll help you out.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

kopzen
Starting Member

34 Posts

Posted - 2010-04-12 : 08:32:14
Thanks TC

Im using SQL 2005 and I dont know how to create or call a function. All I know is how to SLECT * FROM TABLE.

Mabye we should begin there.

So what should I run first and what will it do?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-12 : 09:35:34
quote:
Originally posted by kopzen

Thanks TC

Im using SQL 2005 and I dont know how to create or call a function. All I know is how to SLECT * FROM TABLE.

Mabye we should begin there.

So what should I run first and what will it do?


No problem lets start go over the basics then.

1) What are you allowed to do to the database? Are you allowed to add tables or add program objects like stored procedures and functions?

2) Are you going to want to do this a lot? Are you trying to automate this or do you just want *something* that you can run to get a list of numbers that haven't been used?

If you just want a script that you can run through management studio to generate the current list of unassigned numbers then you can just copy and paste the below sql code (it should work if you run it against your database and it doesn't add any tables or objects (the only reason that it is so long and complicated is because you don't have a tally table or the function that will produce a virtual tally table) But just cut and paste the below code and see if it works for you. If it doesn't please say exactly what you were doing and what the error was.:

; WITH powers AS (
SELECT
[N01], [N02], [N03]
FROM
(
SELECT [N01] = 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11
UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15
) NO1

CROSS JOIN (
SELECT [N02] = 0 UNION SELECT 16 UNION SELECT 32 UNION SELECT 48
UNION SELECT 64 UNION SELECT 80 UNION SELECT 96 UNION SELECT 112
UNION SELECT 128 UNION SELECT 144 UNION SELECT 160 UNION SELECT 176
UNION SELECT 192 UNION SELECT 208 UNION SELECT 224 UNION SELECT 240
) NO2

CROSS JOIN (
SELECT [N03] = 0 UNION SELECT 256 UNION SELECT 512 UNION SELECT 768
UNION SELECT 1024 UNION SELECT 1280 UNION SELECT 1536 UNION SELECT 1792
UNION SELECT 2048 UNION SELECT 2304 UNION SELECT 2560 UNION SELECT 2816
UNION SELECT 3072 UNION SELECT 3328 UNION SELECT 3584 UNION SELECT 3840
) N03
)

, NUMBER_RANGE AS (

SELECT TOP 100 PERCENT
[number] = ( a.[number] + b.[number] ) + 2000
FROM
(
SELECT TOP 100 PERCENT
[number] = CAST([N01] + [N02] + [N03] AS INT)
FROM
powers
WHERE
[N01] + [N02] + [N03] < CAST(CEILING(SQRT(ABS(2001))) AS INT)
ORDER BY
1
) a
CROSS JOIN (
SELECT TOP 100 PERCENT
[number] = CAST(([N01] + [N02] + [N03]) * CAST(CEILING(SQRT(ABS(2001 ))) AS INT) AS INT)
FROM
powers
WHERE
[N01] + [N02] + [N03] < CAST(CEILING(SQRT(ABS(2001 ))) AS INT)
ORDER BY
1
) b
WHERE
a.[number] + b.[number] < ABS(2001)
AND (16777216 - ABS(1999)) > 0
)

select n.[number]
FROM
number_range n
WHERE
NOT EXISTS (SELECT 1 FROM users u WHERE UserDIDNumber = [number])



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
    Next Page

- Advertisement -