on 08-25-2014 9:30 AM
Hi All Experts,
I am using DENSE_RANK Function in a Query Which is Running properly in SSMS,
But when I am running it in Query Generator it is giving Error:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Must specify table to select from.[Microsoft][SQL Server Native Client 10.0][SQL Server]Statement 'Received Alerts' (OAIB) (s) could not be prepared.
Can Any One Please Help me To Get Problem Solved Without Using Dense_Rank Function.
Regards,
Gayatri Shukla.
Hi,
Try to make Procedure and call in the query manager
Regards,
Prasad
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Radek & Prasad,
Thank you For your Solution,Actually I am Using DENSE_Rank Function So that Each time I Run the Query The Ranking Should Start From 1.
As u both said to Create Function And Then Run the Query,I Did the Same But
When I am Running Query giving Date Selection,
then I Get Like this result
DoCnum ItemCode Docdate Rank
1415001 A 01/06/2014 28
1415001 A 01/06/2014 28
1415002 B 03/06/2014 33
1415002 B 03/06/2014 33
1415002 B 03/06/2014 33
1415003 C 01/06/2014 50
But Actual Result Should Be
DoCnum ItemCode Docdate Rank
1415001 A 01/06/2014 1
1415001 A 01/06/2014 1
1415002 B 03/06/2014 2
1415002 B 03/06/2014 2
1415002 B 03/06/2014 2
1415003 C 01/06/2014 3
Note: I Am Getting Above Result Because in Function Particular DocNum has got that Rank.
But Each time if I Run Query, Rank Should Start With 1 And So on
Hope you get My point
Thanks & Regards
Gayatri Shukla
Hi
I guess that the problem arises when you're using this function like this:
select * from dbo.Rank_v2() where docdate > '20070101' and docdate <= '20080101'
but instead you should modify your function to accept parameters and modify your query within the function accordingly. Something like this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter FUNCTION dbo.Rank_v2
(
@data1 datetime = null, @data2 datetime = null
)
RETURNS
@doksRank TABLE
(
[dok] nvarchar(20), [item] nvarchar(50), [docdate] datetime, [Kwota] numeric(19,6), [Rank] int
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
insert into @doksRank
SELECT B.DocNum
,A.ItemCode
,B.DocDate
,A.Quantity
,DENSE_RANK() OVER (
ORDER BY B.DocNum
) AS Rank1
FROM INV1 A
INNER JOIN OINV B ON A.DocEntry = B.DocEntry
where b.DocDate >= @data1 and b.DocDate <= @data2
RETURN
END
GO
In Query Manager you can call this function using query like this:
select * from dbo.Rank_v2('20070101', '20080101')
Kind regards,
Radek
Hi,
What you mean by dense _rank function? Please post your complete query here to check.
Thanks & Regards,
Nagarajan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
You may try to create function in SSMS and call this function from query manager.
For example:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.Rank_v2
(
)
RETURNS
@doksRank TABLE
(
[dok] nvarchar(20), [item] nvarchar(50), [Kwota] numeric(19,6), [Rank] int
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
insert into @doksRank
SELECT B.DocNum
,A.ItemCode
,A.Quantity
,DENSE_RANK() OVER (
ORDER BY B.DocNum
) AS Rank1
FROM INV1 A
INNER JOIN OINV B ON A.DocEntry = B.DocEntry
RETURN
END
GO
Then you can call this function in query manager like this:
select * from dbo.Rank_v2()
and you get your output.
Kind regards,
Radek
User | Count |
---|---|
110 | |
12 | |
11 | |
6 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.