Wednesday, March 28, 2012
Parms
I created a proc which looked liked the following:
ALTER proc dbo.Proc_QuerySkaters
@.ClubID uniqueidentifier =null,
@.msg nvarchar(220) out
as
declare @.cnt int
if @.ClubID = null
begin
select ClubID, FirstName, MiddleName, LastName
from dbo.Skater
set @.cnt = @.@.ROWCOUNT
end
else
begin
select ClubID, FirstName, MiddleName, LastName
from dbo.Skater
where @.clubID = ClubID
set @.cnt = @.@.ROWCOUNT
end
if @.@.ERROR > 0
begin
set @.msg = 'Unexpected error occurred executing Proc_QuerySkaters
- Error code: ' + cast( @.@.ERROR as char(4) )
return -1
end
set @.msg=''
return 0
This compiles, but it does not run correctly. If I change the
declaration of @.ClubID and removed the '=null' - then it compiles, but
it requires the @.ClubID to be populated (will not accept nulls).
How do I define an input parm which is not required to be populated
(accepts nulls and is defaulted to null)?Hi Jim.
The parameter is defined correctly - you should check for null by using IS
NULL rather then = null in the body of the proc:
ALTER proc dbo.Proc_QuerySkaters
@.ClubID uniqueidentifier =null,
@.msg nvarchar(220) out
as
declare @.cnt int
if @.ClubID is null
begin
select ClubID, FirstName, MiddleName, LastName
from dbo.Skater
set @.cnt = @.@.ROWCOUNT
end
else
begin
select ClubID, FirstName, MiddleName, LastName
from dbo.Skater
where @.clubID = ClubID
set @.cnt = @.@.ROWCOUNT
end
if @.@.ERROR > 0
begin
set @.msg = 'Unexpected error occurred executing Proc_QuerySkaters
- Error code: ' + cast( @.@.ERROR as char(4) )
return -1
end
set @.msg=''
return 0
Regards,
Greg Linwood
SQL Server MVP
"Jim Heavey" <JimHeavey@.nospam.com> wrote in message
news:Xns9458D9954D842JimHeaveyhotmailcom@.207.46.248.16...
> I was wondering how to default an input parm to nulls.
> I created a proc which looked liked the following:
> ALTER proc dbo.Proc_QuerySkaters
> @.ClubID uniqueidentifier =null,
> @.msg nvarchar(220) out
> as
> declare @.cnt int
> if @.ClubID = null
> begin
> select ClubID, FirstName, MiddleName, LastName
> from dbo.Skater
> set @.cnt = @.@.ROWCOUNT
> end
> else
> begin
> select ClubID, FirstName, MiddleName, LastName
> from dbo.Skater
> where @.clubID = ClubID
> set @.cnt = @.@.ROWCOUNT
> end
> if @.@.ERROR > 0
> begin
> set @.msg = 'Unexpected error occurred executing Proc_QuerySkaters
> - Error code: ' + cast( @.@.ERROR as char(4) )
> return -1
> end
> set @.msg=''
> return 0
> This compiles, but it does not run correctly. If I change the
> declaration of @.ClubID and removed the '=null' - then it compiles, but
> it requires the @.ClubID to be populated (will not accept nulls).
> How do I define an input parm which is not required to be populated
> (accepts nulls and is defaulted to null)?
Friday, March 23, 2012
Partitioning "Alter Table Switch" Statement Failing
Help!! I can't seem to find information on the error that I'm getting anywhere:
ALTER TABLE SWITCH statement failed. Range defined by partition 1 in table 'DB1.dbo.Table1' is not a subset of range defined by partition 4 in table 'DB1.dbo.Table2'.
Here's some sample code that generates this error
Code Snippet
CREATE PARTITION FUNCTION [Table1Range](int) AS RANGE LEFT FOR VALUES (443, 444, 445)
CREATE PARTITION FUNCTION [Table2Range](int) AS RANGE LEFT FOR VALUES (440, 441, 442, 443)
GO
CREATE PARTITION SCHEME [Table1Scheme] AS PARTITION [Table1Range] TO ([PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY])
CREATE PARTITION SCHEME [Table2Scheme] AS PARTITION [Table2Range] TO ([PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY])
GO
CREATE TABLE [dbo].[Table1](
[session_id] [int] NOT NULL,
[ProcessLogID] [int] NOT NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[session_id] ASC,
[ProcessLogID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [Table1Scheme]([ProcessLogID])
) ON [Table1Scheme]([ProcessLogID])
CREATE TABLE [dbo].[Table2](
[session_id] [int] NOT NULL,
[ProcessLogID] [int] NOT NULL,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
[session_id] ASC,
[ProcessLogID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [Table2Scheme]([ProcessLogID])
) ON [Table2Scheme]([ProcessLogID])
GO
insert into [Table1]
select 1, 443
insert into [Table1]
select 2, 444
insert into [Table1]
select 3, 445
insert into [Table2]
select 4, 440
insert into [Table2]
select 5, 441
insert into [Table2]
select 6, 442
ALTER TABLE [Table1] SWITCH PARTITION 1 to [Table2] PARTITION 4
I'd really appreciate any advice anyone has! Thanks so much.
Jess
Some further info on this
I discovered some minor changes to the example above that would get this working. I don't understand why, on a conceptual level, these changes would make a difference. Any ideas?
The 2 changes that make my above code work:
1)Declare the partition ranges using RIGHT instead of LEFT:
(i.e.
CREATE PARTITION FUNCTION [Table1Range](int) AS RANGE RIGHT FOR VALUES (443, 444, 445)
CREATE PARTITION FUNCTION [Table2Range](int) AS RANGE RIGHT FOR VALUES (440, 441, 442, 443))
2) Run the Alter Table Switch statements on Partition #s 2 & 5 instead of #s 1 & 4
(i.e. "ALTER TABLE [Table1] SWITCH PARTITION 2 to [Table2] PARTITION 5")
It's going to be a ROYAL PITA to switch the partition functions from RIGHT to LEFT in the db I'm working with. Is there anyway I can get this working keeping the LEFT definition?
Thanks!
Jess
|||Alright- I've answered my own question. I wanted to post in case anyone runs into the same error.
I've got it working using the LEFT boundary condition by adding a Check Constraint on Table1.
ALTER TABLE Table1
ADD CONSTRAINT [CK_ProcessLogID]
CHECK ([ProcessLogID] >= 443)
Basically, when I declared the Partition Function connected to table 1 as:
CREATE PARTITION FUNCTION [Table1Range](int) AS RANGE LEFT FOR VALUES (443, 444, 445)
I am declaring Partion Number 1 to store all data where the ProcessLogID<= 443. The key is the LESS THAN or = 443. I was getting confused because in this particular example there was no data that was less than 443 in the table- but there was nothing in the table definition that prohibited it.
Partition Number 4 of Table2 is defined to be all data where the ProcessLogID> = 443 and ProcessLogID < 444 (or ProcessLogID = 443, since it is an integer column). Since a switch statement is actually just altering metadata there can be no data validation, and the definition of the table needs to be representitive that switching one partition to another will follow with the table's partition definition. By adding the check constraint to Table1, you can be assured that all data in Partition #1 of Table1 will be consistant with Partition #4 of Table2. Yippee!
Wednesday, March 21, 2012
Parse error
Can anyone tel lme why this does not parse in 2000 but works fine in 2005?
ALTER TABLE Segments ADD CONSTRAINT
PK_Segments PRIMARY KEY CLUSTERED
(
SegmentsUniqueID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
thanks.
The WITH clause for constraints is new to SQL Server 2005. You can set some of the options in SQL2000 using sp_indexoption or create index.|||Thanks
Parse error
Can anyone tel lme why this does not parse in 2000 but works fine in 2005?
ALTER TABLE Segments ADD CONSTRAINT
PK_Segments PRIMARY KEY CLUSTERED
(
SegmentsUniqueID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
thanks
The WITH options cannot be set like this in SQL Server 2000. The ALLOW_ROW_LOCKS and ALLOW_PAGE_LOCKS options for example cannot be used like this, you need the sp_indexoption for that (however these are on by default).Saturday, February 25, 2012
Parameters
ie: Select * from table1 where ID = request("ID")
What I am doing is opening a report from a webpage and printing out what is displayed in a report. Some are straight forward that are just a listing of information where others the results are based on a unique value like an ID that is passed in the URL. Is there anyway to pass that to the report as well and filter the information based on a request or something along those lines?
Thanks in advance!
Hmm, I don't know if I understand you but when designing your report, create dataset with query: "select * from table1 where ID = @.ID" then in report parameters define @.ID parameter (it can be "static" or depend on another dataset).
Next, before you execute report you must assign to your parameter a value. In Rerpoting Services portal you have GUI that allow user to do so. You can also create GUI for your report parameters on your web page and send parameter values in URL.
Maciej