July 1, 2009 at 1:04 am
I have table named Ranges
CREATE TABLE [dbo].[RANGES](
[Name] [varchar](50) NOT NULL,
[From] [int] NULL,
[To] [int] NULL
)
then i want to use function that get the range of numbers between each from, to value
I typed this query using cross join
select * from RANGES
cross join
(select * from dbo.fnCrossJoinRange2([Ranges.from],[Ranges.to]))t
dbo.fnCrossJoinRange2 is a table value function that I use
this error appear after running the query
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Ranges.from'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Ranges.to'.
I want to solve that without using cursors or loop
any suggestion please
July 1, 2009 at 1:11 am
To pass columns from a table as parameters to a table-valued function in the same FROM clause, you need CROSS APPLY, not CROSS JOIN
select <Column list >
from RANGES
CROSS APPLY dbo.fnCrossJoinRange2([Ranges.from],[Ranges.to]) t
Without seeing the function code, I can't tell if this will do what you want or not, but test it and see.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 1, 2009 at 1:18 am
GilaMonster (7/1/2009)
To pass columns from a table as parameters to a table-valued function in the same FROM clause, you need CROSS APPLY, not CROSS JOIN
select <Column list >
from RANGES
CROSS APPLY dbo.fnCrossJoinRange2([Ranges.from],[Ranges.to]) t
Without seeing the function code, I can't tell if this will do what you want or not, but test it and see.
thanks for ur help the code of the function can be found :
CREATE VIEW dbo.Digits AS
select 0 as value
union allselect 1 as value
union allselect 2 as value
union allselect 3 as value
union allselect 4 as value
union allselect 5 as value
union allselect 6 as value
union allselect 7 as value
union allselect 8 as value
union allselect 9 as value
GO
ALTER FUNCTION dbo.fnCrossJoinRange2 (
@first int , --##PARAM @first The lowest value in the range.
@Last int --##PARAM @Last The highest value in the range.
)
RETURNS @values TABLE ( value int primary key ) AS
BEGIN
INSERT INTO @values(value)
SELECTnum = units.value +
(tens.value) +
(hundreds.value ) +
(Thousands.value ) +
(TenThousands.value ) +
(CThousands.value ) +
(Millions.value )
FROMdbo.Digits units
CROSS JOIN (SELECT value * 10 as value from dbo.Digits WHERE value * 10 <=@last) tens
CROSS JOIN (SELECT value * 100 as value from dbo.Digits WHERE value * 100 <=@last) hundreds
CROSS JOIN (SELECT value * 1000 as value from dbo.Digits WHERE value * 1000 <=@last) Thousands
CROSS JOIN (SELECT value * 10000 as value from dbo.Digits WHERE value * 10000 <=@last) TenThousands
CROSS JOIN (SELECT value * 100000 as value from dbo.Digits WHERE value * 100000 <=@last) CThousands
CROSS JOIN (SELECT value * 1000000 as value from dbo.Digits WHERE value * 1000000 <=@last) Millions
where units.value +
(tens.value ) +
(hundreds.value) +
(Thousands.value ) +
(TenThousands.value ) +
(CThousands.value ) +
(Millions.value )
BETWEEN @first and @Last
RETURN
END
GO
I used ur code as:
select *
from RANGES CROSS APPLY dbo.fnCrossJoinRange2([Ranges.from],[Ranges.to])
but give the same error
any help in that
July 1, 2009 at 1:31 am
ali.m.habib (7/1/2009)
select *from RANGES CROSS APPLY dbo.fnCrossJoinRange2([Ranges.from],[Ranges.to])
but give the same error
Is the database case sensitive? If so, you'll need to fix the case in the proc's parameters.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 1, 2009 at 1:37 am
GilaMonster (7/1/2009)
ali.m.habib (7/1/2009)
select *from RANGES CROSS APPLY dbo.fnCrossJoinRange2([Ranges.from],[Ranges.to])
but give the same error
Is the database case sensitive? If so, you'll need to fix the case in the proc's parameters.
thanx alot but the output not give the range , it just repeated the data
the data in ranges is
Ali15
Ali2450
I want the out put to be
Ali1
Ali 2
Ali 3
.............
Ali2 4
Ali
Ali2
July 1, 2009 at 1:38 am
GilaMonster (7/1/2009)
ali.m.habib (7/1/2009)
select *from RANGES CROSS APPLY dbo.fnCrossJoinRange2([Ranges.from],[Ranges.to])
but give the same error
Is the database case sensitive? If so, you'll need to fix the case in the proc's parameters.
thanx alot but the output not give the range , it just repeated the data
the data in ranges is
Ali15
Ali2450
I want the out put to be
Ali1
Ali 2
Ali 3
.............
Ali2 4
Ali2 5
...............
Ali250
the output I got from the qur\ery was:
Ali15
Ali15
Ali15
Ali15
Ali15
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
Ali2450
do u have any idea to fix that
July 1, 2009 at 1:59 am
Can you give us some sample data for the Ranges table? Read this to see the best way to post this to get quick responses.
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 1, 2009 at 2:08 am
GilaMonster (7/1/2009)
Can you give us some sample data for the Ranges table? Read this to see the best way to post this to get quick responses.http://www.sqlservercentral.com/articles/Best+Practices/61537/
The Ranges Table have the following Data
Name From To
------------------------
Ali 1 5
Ali2 4 50
I want the out put to be the name and the data range between from and to column numbers
Ali 1
Ali 2
Ali 3
.................
July 1, 2009 at 2:27 am
So want you want outputting is
Ali, 1
Ali, 2
Ali, 3
Ali, 4
Ali, 5
Ali2, 5
Ali2, 6
Ali2, 7
... all the way up to
Ali2, 49
Ali2, 50
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 1, 2009 at 3:01 am
GilaMonster (7/1/2009)
So want you want outputting isAli, 1
Ali, 2
Ali, 3
Ali, 4
Ali, 5
Ali2, 5
Ali2, 6
Ali2, 7
... all the way up to
Ali2, 49
Ali2, 50
yes exactly what I want , please help me in that
July 1, 2009 at 4:03 am
Assuming 2005 +
create table ali
(
code varchar(16),
fromcol integer,
toCol integer
)
go
insert into ali values('ali1',1,5)
insert into ali values('ali2',6,15)
go
with ctenumber(rownum)
as
(
Select rownum = row_number() over(order by id)
from sysobjects
)
select * from ali,
ctenumber
where rownum between fromcol and tocol
July 1, 2009 at 4:51 am
Dave Ballantyne (7/1/2009)
Assuming 2005 +
create table ali
(
code varchar(16),
fromcol integer,
toCol integer
)
go
insert into ali values('ali1',1,5)
insert into ali values('ali2',6,15)
go
with ctenumber(rownum)
as
(
Select rownum = row_number() over(order by id)
from sysobjects
)
select * from ali,
ctenumber
where rownum between fromcol and tocol
thanks alot for you code it worked well, but is there any article to describe the code
with ctenumber(rownum)
as
(
Select rownum = row_number() over(order by id)
from sysobjects
)
Also
it not work for high numbers
July 1, 2009 at 5:45 am
Which part are you particularly needing help with the cte (common table expression ) or row_number() ?
Google is your friend ...
Also
it not work for high numbers
If will work for as many rows are returned. If you need high numbers, there are many different solutions.
I personally have a calendar table with a dayoffset integer that i use. You could cross join to sysobjects to itself . The source data dosent really matter , its just a way of getting an incrementing number.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply