Showing posts with label dbo. Show all posts
Showing posts with label dbo. Show all posts

Friday, March 30, 2012

increase the speed of the report

Hello,

I am working on a report in SQL Server Reporting Services 2000.

[CODE]
SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE [Call Day] = case when @.callDate = '' then [Call Day] else @.callDate end
[/CODE]

>> I have apromt for the user to enter the date.
>> If the user does not enter any date, then the report will show all the first 200 records.
>> This query is running too slow.

To increase the speed of the report , could somebody help me build the where clause only when something is in the filters ?

Thank you,

This should do what you want:

SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE [Call Day] = @.callDate

|||

I tried using

SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE [Call Day] = @.callDate

>> Output is blank.

>> I need the top 200 records to be returned by default. If the @.callday is blank.

Thank you

|||

urpalshu wrote:

I tried using

SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE [Call Day] = @.callDate

>> Output is blank.

>> I need the top 200 records to be returned by default. If the @.callday is blank.

Thank you

You need to use boolean logic here, you stated in some cases no date is entered.

By the way I hope call day is of type date time...

In any even if you sometimes have a value for @.callDate and other times it is null the sproc should be this:

@.callDate datetime= NULL --do you need a default ?

SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE ([Call Day] = @.callDate OR @.callDate IS NULL)

Make sure Call Day is of the right type (datetime). If it is varchar, you will need to change the data type. You can strip the day time month year using various date functions.

Jon

|||

Thank you,

I changed the Call Date to datetime,

if @.callDate IS NULL AND @.destNbr = '' AND @.origNbr = '' AND @.btn = '' AND @.invoiceNbr = '' AND @.destMobile = ''
BEGIN
SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
END
ELSE
BEGIN
SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE ([Call Day] = @.callDate OR @.callDate IS NULL)
AND [Dest Nbr] = case when @.destNbr = '' then [Dest Nbr] else @.destNbr end
AND [Orig Nbr] = case when @.origNbr = '' then [Orig Nbr] else @.origNbr end
AND [BTN] = case when @.btn = '' then [BTN] else @.btn end
AND [invoice nbr] = case when @.invoiceNbr = '' then [invoice nbr] else @.invoiceNbr end
AND [dest mobile] = case when @.destMobile = '' then [dest mobile] else @.destMobile end
END

Can we improve the speed on this query?

Please help

|||

This is a quite common type of query requirement when coding queries that do searches.

What you want to do is replicate the exact same logic you used for the @.callDate parameter for all the other parameters too:

SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE ([Call Day] = @.callDate OR @.callDate IS NULL)
AND (@.destNbr is null or @.destNbr = '' or [Dest Nbr] = @.destNbr)
AND (@.origNbr is null or @.origNbr = '' or [Orig Nbr] = @.origNbr)
... etc...

Notice the bracketing to group the expressions (it won't work correctly without it), and the ordering of the expressions: we are relying on shortcutting to ensure that the field vs. parameter check is only evaluated if the parameter contains a legitimate value (ie isn't empty).

If you do it this way then you can also get rid of the outer if @.callDate is null and @.destNbr = '' etc...

sluggy

|||

sluggy wrote:

This is a quite common type of query requirement when coding queries that do searches.

What you want to do is replicate the exact same logic you used for the @.callDate parameter for all the other parameters too:

SELECT TOP 200 * FROM necc.dbo.vw_rop_report_profit_per_call
WHERE ([Call Day] = @.callDate OR @.callDate IS NULL)
AND (@.destNbr is null or @.destNbr = '' or [Dest Nbr] = @.destNbr)
AND (@.origNbr is null or @.origNbr = '' or [Orig Nbr] = @.origNbr)
... etc...

Notice the bracketing to group the expressions (it won't work correctly without it), and the ordering of the expressions: we are relying on shortcutting to ensure that the field vs. parameter check is only evaluated if the parameter contains a legitimate value (ie isn't empty).

If you do it this way then you can also get rid of the outer if @.callDate is null and @.destNbr = '' etc...

sluggy

Its amazing how people dont listen, did I not just post this like the third post ?

|||

You sure did, but the original poster was still stuck, so i expanded upon it for him. You will see i mentioned "what he had already done with the @.callDate parameter" - this acknowledges your post.

But this is not the place for a flame war, so let it go.

sluggy

Monday, March 26, 2012

Incorrect syntax near the keyword 'OR'.

Hi,
I have a stored procedure
CREATE PROCEDURE dbo.Retrieve
(
@.SEARCH_STRING nvarchar(200),
@.COUNT int
)
AS
DECLARE @.STRING_COUNT varchar(3)
DECLARE @.SQL varchar(1000)
SET @.STRING_COUNT = CAST(@.COUNT AS varchar(3))
SET @.SQL='SELECT TOP ' + @.STRING_COUNT + '[ID] FROM [EMPLOYEES]
WHERE ([NAME] LIKE ' + @.SEARCH_STRING + '% OR [EMPLOYEE_REFERENCE] LIKE ' +
@.SEARCH_STRING + '% )'
EXEC (@.SQL)
The stored procedure is created successfully.
But I get the error when I try to use it: (Retrieve '',10)
Server: Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'OR'.
Thanks
KiranAnswered in .programming. Please don't multi-post.
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Kiran" <Kiran@.nospam.net> wrote in message
news:O9oiPrX#EHA.2876@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I have a stored procedure
> CREATE PROCEDURE dbo.Retrieve
> (
> @.SEARCH_STRING nvarchar(200),
> @.COUNT int
> )
> AS
> DECLARE @.STRING_COUNT varchar(3)
> DECLARE @.SQL varchar(1000)
>
> SET @.STRING_COUNT = CAST(@.COUNT AS varchar(3))
> SET @.SQL='SELECT TOP ' + @.STRING_COUNT + '[ID] FROM [EMPLOYEES]
> WHERE ([NAME] LIKE ' + @.SEARCH_STRING + '% OR [EMPLOYEE_REFERENCE] LIKE '
+
> @.SEARCH_STRING + '% )'
> EXEC (@.SQL)
> The stored procedure is created successfully.
> But I get the error when I try to use it: (Retrieve '',10)
> Server: Msg 156, Level 15, State 1, Line 2
> Incorrect syntax near the keyword 'OR'.
>
> Thanks
> Kiran
>sql

Wednesday, March 21, 2012

Incorrect Login associated with dbo

If I run the following statement and do not get any returns, does that mean I
have an incorrect login associated with the dbo?
use MY_DATABASE
go
select d.name, dp.name, d.owner_sid, dp.sid from sys.databases d,
sys.database_principals dp where d.owner_sid = dp.sid and dp.name = 'dbo'
and d.name = 'MY_DATABASE'
Message posted via droptable.com
http://www.droptable.com/Uwe/Forums.aspx/sql-server/200703/1
> If I run the following statement and do not get any returns, does that
> mean I
> have an incorrect login associated with the dbo?
Yes. The dbo user sid should normally match the owner_sid in sys.databases.
The query below will should the names, if valid.
SELECT
d.name AS database_name,
SUSER_SNAME(d.owner_sid) AS databases_owner,
SUSER_SNAME(dp.sid) AS dbo_login
FROM sys.databases d
CROSS JOIN sys.database_principals dp
WHERE
d.name = 'MY_DATABASE'
AND dp.name = 'dbo'
You can correct the mismatch with ALTER AUTHORIZATION. For example:
ALTER AUTHORIZATION ON DATABASE::MY_DATABASE TO [sa];
Hope this helps.
Dan Guzman
SQL Server MVP
"cbrichards via droptable.com" <u3288@.uwe> wrote in message
news:6f875a7486c91@.uwe...
> If I run the following statement and do not get any returns, does that
> mean I
> have an incorrect login associated with the dbo?
> use MY_DATABASE
> go
> select d.name, dp.name, d.owner_sid, dp.sid from sys.databases d,
> sys.database_principals dp where d.owner_sid = dp.sid and dp.name = 'dbo'
> and d.name = 'MY_DATABASE'
> --
> Message posted via droptable.com
> http://www.droptable.com/Uwe/Forums.aspx/sql-server/200703/1
>

Monday, March 19, 2012

Incorrect Login associated with dbo

If I run the following statement and do not get any returns, does that mean
I
have an incorrect login associated with the dbo?
use MY_DATABASE
go
select d.name, dp.name, d.owner_sid, dp.sid from sys.databases d,
sys.database_principals dp where d.owner_sid = dp.sid and dp.name = 'dbo'
and d.name = 'MY_DATABASE'
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...server/200703/1> If I run the following statement and do not get any returns, does that
> mean I
> have an incorrect login associated with the dbo?
Yes. The dbo user sid should normally match the owner_sid in sys.databases.
The query below will should the names, if valid.
SELECT
d.name AS database_name,
SUSER_SNAME(d.owner_sid) AS databases_owner,
SUSER_SNAME(dp.sid) AS dbo_login
FROM sys.databases d
CROSS JOIN sys.database_principals dp
WHERE
d.name = 'MY_DATABASE'
AND dp.name = 'dbo'
You can correct the mismatch with ALTER AUTHORIZATION. For example:
ALTER AUTHORIZATION ON DATABASE::MY_DATABASE TO [sa];
Hope this helps.
Dan Guzman
SQL Server MVP
"cbrichards via droptable.com" <u3288@.uwe> wrote in message
news:6f875a7486c91@.uwe...
> If I run the following statement and do not get any returns, does that
> mean I
> have an incorrect login associated with the dbo?
> use MY_DATABASE
> go
> select d.name, dp.name, d.owner_sid, dp.sid from sys.databases d,
> sys.database_principals dp where d.owner_sid = dp.sid and dp.name = 'dbo'
> and d.name = 'MY_DATABASE'
> --
> Message posted via droptable.com
> http://www.droptable.com/Uwe/Forum...server/200703/1
>

Incorrect Login associated with dbo

If I run the following statement and do not get any returns, does that mean I
have an incorrect login associated with the dbo?
use MY_DATABASE
go
select d.name, dp.name, d.owner_sid, dp.sid from sys.databases d,
sys.database_principals dp where d.owner_sid = dp.sid and dp.name = 'dbo'
and d.name = 'MY_DATABASE'
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200703/1> If I run the following statement and do not get any returns, does that
> mean I
> have an incorrect login associated with the dbo?
Yes. The dbo user sid should normally match the owner_sid in sys.databases.
The query below will should the names, if valid.
SELECT
d.name AS database_name,
SUSER_SNAME(d.owner_sid) AS databases_owner,
SUSER_SNAME(dp.sid) AS dbo_login
FROM sys.databases d
CROSS JOIN sys.database_principals dp
WHERE
d.name = 'MY_DATABASE'
AND dp.name = 'dbo'
You can correct the mismatch with ALTER AUTHORIZATION. For example:
ALTER AUTHORIZATION ON DATABASE::MY_DATABASE TO [sa];
Hope this helps.
Dan Guzman
SQL Server MVP
"cbrichards via SQLMonster.com" <u3288@.uwe> wrote in message
news:6f875a7486c91@.uwe...
> If I run the following statement and do not get any returns, does that
> mean I
> have an incorrect login associated with the dbo?
> use MY_DATABASE
> go
> select d.name, dp.name, d.owner_sid, dp.sid from sys.databases d,
> sys.database_principals dp where d.owner_sid = dp.sid and dp.name = 'dbo'
> and d.name = 'MY_DATABASE'
> --
> Message posted via SQLMonster.com
> http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200703/1
>

Monday, March 12, 2012

Inconsistent SQL results

Hi

I have an oddity. If I run a piece of SQL:

SELECT EmployeeNo, MailTo
FROM ST_PPS.dbo.Employee
where AddedOn BETWEEN '01-jan-2006' and '01-feb-2006'
AND MailTo NOT IN ( '3', 'x')
order by MailTo

I get the results

EmployeeNo MailTo
---- --
608384 1
606135 1
608689 1
609095 1
607163 1
606165 1
606472 1
608758 1
....

for 2594 rows

If I create a stored procedure with the same SQL:-

CREATE PROCEDURE dbo.PPS_test
AS
SELECT EmployeeNo, MailTo
FROM ST_PPS.dbo.Employee
where AddedOn BETWEEN '01-jan-2006' and '01-feb-2006'
AND MailTo NOT IN ( '3', 'x')
order by MailTo
GO

and run it:-

EXEC PPS_test

I get three extra rows

EmployeeNo MailTo
---- --
607922 NULL
606481 NULL
605599 NULL
606316 1
608871 1
607427 1
608795 1
....
for 2597

Does anyone know what is happening here? It appears that the clause:-

MailTo NOT IN ( '3', 'x')

excludes NULL in raw SQL, but includes NULL (correctly I think) in a
stored procedure.

Chloe Crowder
The British Libraryverify your ansi_nulls settings:

create table #t(i int)
insert into #t
select null
union all
select 1
union all
select 2
union all
select 3
go
select i from #t where i not in(1)

i
----
2
3

(2 row(s) affected)
go
set ansi_nulls off
go
select i from #t where i not in(1)

i
----
NULL
2
3

(3 row(s) affected)

go
drop table #t|||chloe.crowder@.bl.uk wrote:
> Hi
> I have an oddity.

Well, I'm not able to duplicate your problem with the sql below. Can
you possibly provide create table and insert statements that show the
strange behavior?

create table Employee (
EmployeeNo int,
MailTo char(1) NULL
)
insert Employee values (608384,'1')
insert Employee values (606135,'1')
insert Employee values (608689,'x')
insert Employee values (609095,'3')
insert Employee values (607922,'2')
insert Employee (EmployeeNo) values (606481)
insert Employee (EmployeeNo) values (605599)

select EmployeeNo, MailTo
from Employee
where MailTo NOT IN ( '3', 'x')
order by MailTo
go

create procedure PPS_test as
select EmployeeNo, MailTo
from Employee
where MailTo NOT IN ( '3', 'x')
order by MailTo
go

exec PPS_test

drop table Employee
drop procedure PPS_test|||(chloe.crowder@.bl.uk) writes:
> If I create a stored procedure with the same SQL:-
> CREATE PROCEDURE dbo.PPS_test
> AS
> SELECT EmployeeNo, MailTo
> FROM ST_PPS.dbo.Employee
> where AddedOn BETWEEN '01-jan-2006' and '01-feb-2006'
> AND MailTo NOT IN ( '3', 'x')
> order by MailTo
> GO
> and run it:-
> EXEC PPS_test
> I get three extra rows
> EmployeeNo MailTo
> ---- --
> 607922 NULL
> 606481 NULL
> 605599 NULL

Let me guess: you are creating your stored procedures in Enterprise
Manager, aren't you? That's a crappy tool to edit stored procedures
in. You are better off using Query Analyzer.

One reason it's crappy is because, it defaults the settings
ANSI_NULLS and QUOTED_IDENTIFIER to be OFF. These settings are
saved with the procedure, so when you run the procedure ANSI_NULLS
is off, and you get three extra rows. Normally, when ANSI_NULLS is
ON (which is the default in most environments), NULL is never
equal to anything, and never is it unequal to anything. But when the
setting is OFF NULLs are equal to other NULLS and unequal to other
values. This is a legacy setting that should be avoided.

There are also features in SQL Server that requires ANSI_NULLS to
be ON, so there is all reason to run with ANSI_NULLS on.

In Query Analyzer, ANSI_NULLS is ON by default.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland

Ah, that would certainly explain it. Don't you just love Microsoft at
times!

Does the 'feature' carry forward into SQL 2005?

Thanks to Eric and Alexander for their time

Chloe|||(chloe.crowder@.bl.uk) writes:
> Ah, that would certainly explain it. Don't you just love Microsoft at
> times!
> Does the 'feature' carry forward into SQL 2005?

The legacy settings SET ANSI_NULLS OFF and SET QUOTED_IDENTIFIER OFF
remains. But in the new Mgmt Studio it is not equally easy to run with
these settings off. (There are alas some cases where they still turn them
off, despite that I have hammered them with bug reports about this.)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Friday, February 24, 2012

include column from table in SELECT in an INSERT OUTPUT clause

Hey All –

I am having problems with the OUTPUT clause in an INSERT.

I have something like this:

INSERT INTO dbo.Person

( PersonID

,Name

)

OUTPUT p.AuditVersionGUID

,inserted.PersonID

,inserted.Name

INTO dbo.PersonAudit

(

AuditVersionGUID

,PersonID

,Name

)

SELECT p.PersonID, p.Name

FROM #Person p

AuditVersionGUID is not and shouldn’t be defined on the Person table but I do have it defined in my #Person table.

I get this error:

Msg 4104, Level 16, State 1, Procedure spExtractPerson, Line 275

The multi-part identifier "P.AuditVersionGUID" could not be bound.

I can accomplish this when I am using the OUTPUT clauses in DELETE and UPDATE statements but not the INSERT.

Is this possible or am I relegated to using a memory table and appending the GUID in a separate statement after the OUTPUT?

Thank you,

Cameron

Yeah the GUID has already been determined since a few other sprocs need to refer to the same one.

Thank you for confiming what i had thought. Seems kind of strange why it only doesnt work on INSERT.

Cameron

|||

It's not allowed. Only { DELETED | INSERTED | from_table_name } . { * | column_name } is allowed in OUTPUT clause. from_table_name is a table included in the FROM clause of a DELETE or UPDATE statement that is used to specify the rows to update or delete.

But the following equivalent statement should work for you.

-- swap PersonAudit and Person

INSERT INTO dbo.PersonAudit

( AuditVersionGUID

,PersonID

,Name

)

OUTPUT

inserted.PersonID

,inserted.Name

INTO dbo.Person

(

PersonID

,Name

)

SELECT p.AuditVersionGUID, p.PersonID, p.Name

FROM #Person p

go

|||Ah, that would work, but Person is in a FK relationship, which OUTPUT does not allow the table to be on any side of a FK relationship.|||

In that case, then, you could always load a variable with the GUID and return it in the OUTPUT clause:

OUTPUT @.MyGUID,

inserted.[PersonID],

inserted.[name]

Obviously I don't understand your complete scenario so this may or may not be of any help.

Chris

|||It is doing a bulk insert so i can't use a variable because each AuditVersionGUID is linked to a specific PersonID, which is why i was trying to get it from the tables in the SELECT|||

In the OUTPUT clause you cannot reference tables other than INSERTED or DELETED when performing an INSERT as you can when performing a DELETE or an UPDATE. Look up the 'OUTPUT Clause' topic in BOL for more info.

Incidentally, in your example code has the value of the GUID already been determined by the time that you use the OUTPUT clause?

If not then you could simply use the following to generate a GUID:

OUTPUT NEWID(),

inserted.[PersonID],

inserted.[name]

Chris