Showing posts with label value. Show all posts
Showing posts with label value. Show all posts

Friday, March 30, 2012

Resolving an attribute member from a member property

What will be the fastest way to resolve an atttribute member from one of its member properties, e.g. the key value from the source database assuming that the attribute key uses the dimension surrogate key for its KeyColumn?

You can use the member properties Key0, Key1, ... KeyN. Each of theses represents one of the columns in the attributes key columns.

E.g.

select [Time By Day].[Years].Members dimension properties key0, key1 on 0
from budget

|||

This is not what I was asking. Consider the following key attribute and member properties

Product (attribute key)

- ProductOriginal Key (member property and/or attribute)

The question was how can I resolve the Product attribute by knowing the ProductOriginalKey member property?

|||

Since there is many-to-one relationship between related attributes, there are actually multiple products with the same ProductOrdinal. You can easily get the set of all Product attribute members which have certain ProductOrdinal value, by using

Exists(Product.Product.Product.MEMBERS, Product.ProductOrdinal.&[key_of_ordinal])

HTH,

Mosha (http://www.mosha.com/msolap)

|||Thank you. Unfortunately, Exists doesn't seem to work as an allowed set security filter with parent-child dimensions as I reported on connect with CTP2 SP2. Neither does crossjoining works (*). A bug perhaps? Meanwhile, any idea what works to crossjoin two sets to create an allowed set?|||Sorry - I didn't see you mentioning before that it was Parent-Child. Can you please give more concrete description of the structure of dimension, which attributes you are securing etc. Example from Adventure Works would be most appreciated.|||

The link in my previous post demonstrates how this can be reproduced with AW. Assuming the AdventureWorks sample cube:


1. Create a new role Reviewer
2. Create an allowed set on the Employee dimension (cube level) as follows:
Exists([Employee].[Employees].Members, [Employee].[Hire Year].&[1997])
3. Browse the cube under Reviewer by Employee. Notice that all employees are returned. In comparison, the following query returns only two employees and their supervisors (as it should).
select [Measures].[Reseller Sales Amount] on 0,
Exists([Employee].[Employees].Members, [Employee].[Hire Year].&[1997]) on 1
from [Adventure Works]


4. Trying [Employee].[Employees].Members * [Employee].[Hire Year].&[1997] throws an exception, as well as
Exists([Employee].[Employees].Members, [Employee].[Hire Year].&[1997], 'Reseller Sales')

|||

Well, since you are defining dimension security - there is no need to ever write Exists as expression of any allowed set. Dimension security will do Exists itself. So all you need to do is to define security on Hire Year attribute instead of trying to do it on Employees attribute. Simply specify [Employee].[Hire Year].&[1997] as expression for the Allowed Set on Hire Year attribute - and everything will work as you need.

HTH,

Mosha (http://www.mosha.com/msolap)

|||

This works! Thank you so much for your help. I'd appreciate it if you could answer one more question for me. Let's say that when the user slices by the Sales Territory Region attribute hierarchy of the Sales Territory dimension, I want the user to see only the regions serviced by the allowed employees only. Is the following allowed set expression the most efficient way to do so?

Exists([Sales Territory].[Sales Territory Region].[Sales Territory Region].Members, [Employee].[Hire Year].&[1997], 'Reseller Sales')

I understand that I cannot use [Employee].[Hire Year].Members since security polices are not applied yet so essentially I have no other choice but to carry the Employee filter to the other dimensions. I am just concerned that with large dimensions this may incur significant performance penalty.

|||Yes, I beleive this is the best way to do it.

Resolving an attribute member from a member property

What will be the fastest way to resolve an atttribute member from one of its member properties, e.g. the key value from the source database assuming that the attribute key uses the dimension surrogate key for its KeyColumn?

You can use the member properties Key0, Key1, ... KeyN. Each of theses represents one of the columns in the attributes key columns.

E.g.

select [Time By Day].[Years].Members dimension properties key0, key1 on 0
from budget

|||

This is not what I was asking. Consider the following key attribute and member properties

Product (attribute key)

- ProductOriginal Key (member property and/or attribute)

The question was how can I resolve the Product attribute by knowing the ProductOriginalKey member property?

|||

Since there is many-to-one relationship between related attributes, there are actually multiple products with the same ProductOrdinal. You can easily get the set of all Product attribute members which have certain ProductOrdinal value, by using

Exists(Product.Product.Product.MEMBERS, Product.ProductOrdinal.&[key_of_ordinal])

HTH,

Mosha (http://www.mosha.com/msolap)

|||Thank you. Unfortunately, Exists doesn't seem to work as an allowed set security filter with parent-child dimensions as I reported on connect with CTP2 SP2. Neither does crossjoining works (*). A bug perhaps? Meanwhile, any idea what works to crossjoin two sets to create an allowed set?|||Sorry - I didn't see you mentioning before that it was Parent-Child. Can you please give more concrete description of the structure of dimension, which attributes you are securing etc. Example from Adventure Works would be most appreciated.|||

The link in my previous post demonstrates how this can be reproduced with AW. Assuming the AdventureWorks sample cube:


1. Create a new role Reviewer
2. Create an allowed set on the Employee dimension (cube level) as follows:
Exists([Employee].[Employees].Members, [Employee].[Hire Year].&[1997])
3. Browse the cube under Reviewer by Employee. Notice that all employees are returned. In comparison, the following query returns only two employees and their supervisors (as it should).
select [Measures].[Reseller Sales Amount] on 0,
Exists([Employee].[Employees].Members, [Employee].[Hire Year].&[1997]) on 1
from [Adventure Works]


4. Trying [Employee].[Employees].Members * [Employee].[Hire Year].&[1997] throws an exception, as well as
Exists([Employee].[Employees].Members, [Employee].[Hire Year].&[1997], 'Reseller Sales')

|||

Well, since you are defining dimension security - there is no need to ever write Exists as expression of any allowed set. Dimension security will do Exists itself. So all you need to do is to define security on Hire Year attribute instead of trying to do it on Employees attribute. Simply specify [Employee].[Hire Year].&[1997] as expression for the Allowed Set on Hire Year attribute - and everything will work as you need.

HTH,

Mosha (http://www.mosha.com/msolap)

|||

This works! Thank you so much for your help. I'd appreciate it if you could answer one more question for me. Let's say that when the user slices by the Sales Territory Region attribute hierarchy of the Sales Territory dimension, I want the user to see only the regions serviced by the allowed employees only. Is the following allowed set expression the most efficient way to do so?

Exists([Sales Territory].[Sales Territory Region].[Sales Territory Region].Members, [Employee].[Hire Year].&[1997], 'Reseller Sales')

I understand that I cannot use [Employee].[Hire Year].Members since security polices are not applied yet so essentially I have no other choice but to carry the Employee filter to the other dimensions. I am just concerned that with large dimensions this may incur significant performance penalty.

|||Yes, I beleive this is the best way to do it.

Wednesday, March 28, 2012

Resizing an existing DB

I created a DB with a preallocated size of 20GB and I need to shink
this down to a smaller size. If I go into DB properties and enter a
new value it says that the size must be greater than the current size.
Is there any way to do this? Also when I try and restore the DB is
wants 20GB of disk space even though it's only using about 1GB of
data. Any help will be muchly appreciated.
Darren.Use DBCC SHRINKDATABASE or DBCC SHRINKFILE. These commands are documented in Books Online.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Darren" <walker_712@.hotmail.com> wrote in message news:cd9d3666.0405102239.21563966@.posting.google.com...
> I created a DB with a preallocated size of 20GB and I need to shink
> this down to a smaller size. If I go into DB properties and enter a
> new value it says that the size must be greater than the current size.
> Is there any way to do this? Also when I try and restore the DB is
> wants 20GB of disk space even though it's only using about 1GB of
> data. Any help will be muchly appreciated.
> Darren.|||Hi Darren,
Unfortunatly you can't. The smallest size you can shrink
your database to is the size the database was initally set
to when you created it, in this case 20gb.
Although there are lots of ways of doing it, personally I
would save the data, drop the database, recreate it with 1
gb, then copy the database back into it.
J
>--Original Message--
>I created a DB with a preallocated size of 20GB and I
need to shink
>this down to a smaller size. If I go into DB properties
and enter a
>new value it says that the size must be greater than the
current size.
>Is there any way to do this? Also when I try and restore
the DB is
>wants 20GB of disk space even though it's only using
about 1GB of
>data. Any help will be muchly appreciated.
>Darren.
>.
>|||Julie,
DBCC SHRINKFILE should be able to shrink the size below the initial size...
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Julie" <anonymous@.discussions.microsoft.com> wrote in message news:b30801c43735$c4a30970$a301280a@.phx.gbl...
> Hi Darren,
> Unfortunatly you can't. The smallest size you can shrink
> your database to is the size the database was initally set
> to when you created it, in this case 20gb.
> Although there are lots of ways of doing it, personally I
> would save the data, drop the database, recreate it with 1
> gb, then copy the database back into it.
> J
>
>
> >--Original Message--
> >I created a DB with a preallocated size of 20GB and I
> need to shink
> >this down to a smaller size. If I go into DB properties
> and enter a
> >new value it says that the size must be greater than the
> current size.
> >Is there any way to do this? Also when I try and restore
> the DB is
> >wants 20GB of disk space even though it's only using
> about 1GB of
> >data. Any help will be muchly appreciated.
> >
> >Darren.
> >.
> >|||Ok, now hanging my head in shame.
Moving swiftly along...
>--Original Message--
>Julie,
>DBCC SHRINKFILE should be able to shrink the size below
the initial size...
>--
>Tibor Karaszi, SQL Server MVP
>http://www.karaszi.com/sqlserver/default.asp
>
>"Julie" <anonymous@.discussions.microsoft.com> wrote in
message news:b30801c43735$c4a30970$a301280a@.phx.gbl...
>> Hi Darren,
>> Unfortunatly you can't. The smallest size you can shrink
>> your database to is the size the database was initally
set
>> to when you created it, in this case 20gb.
>> Although there are lots of ways of doing it,
personally I
>> would save the data, drop the database, recreate it
with 1
>> gb, then copy the database back into it.
>> J
>>
>>
>> >--Original Message--
>> >I created a DB with a preallocated size of 20GB and I
>> need to shink
>> >this down to a smaller size. If I go into DB properties
>> and enter a
>> >new value it says that the size must be greater than
the
>> current size.
>> >Is there any way to do this? Also when I try and
restore
>> the DB is
>> >wants 20GB of disk space even though it's only using
>> about 1GB of
>> >data. Any help will be muchly appreciated.
>> >
>> >Darren.
>> >.
>> >
>
>.
>|||LOL...
As if we didn't do this all of us, from time to time. :-)
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Julie" <anonymous@.discussions.microsoft.com> wrote in message news:b38e01c4373e$eb86b330$a301280a@.phx.gbl...
> Ok, now hanging my head in shame.
> Moving swiftly along...
>
> >--Original Message--
> >Julie,
> >
> >DBCC SHRINKFILE should be able to shrink the size below
> the initial size...
> >
> >--
> >Tibor Karaszi, SQL Server MVP
> >http://www.karaszi.com/sqlserver/default.asp
> >
> >
> >"Julie" <anonymous@.discussions.microsoft.com> wrote in
> message news:b30801c43735$c4a30970$a301280a@.phx.gbl...
> >> Hi Darren,
> >>
> >> Unfortunatly you can't. The smallest size you can shrink
> >> your database to is the size the database was initally
> set
> >> to when you created it, in this case 20gb.
> >>
> >> Although there are lots of ways of doing it,
> personally I
> >> would save the data, drop the database, recreate it
> with 1
> >> gb, then copy the database back into it.
> >>
> >> J
> >>
> >>
> >>
> >>
> >> >--Original Message--
> >> >I created a DB with a preallocated size of 20GB and I
> >> need to shink
> >> >this down to a smaller size. If I go into DB properties
> >> and enter a
> >> >new value it says that the size must be greater than
> the
> >> current size.
> >> >Is there any way to do this? Also when I try and
> restore
> >> the DB is
> >> >wants 20GB of disk space even though it's only using
> >> about 1GB of
> >> >data. Any help will be muchly appreciated.
> >> >
> >> >Darren.
> >> >.
> >> >
> >
> >
> >.
> >

Resizing an existing DB

I created a DB with a preallocated size of 20GB and I need to shink
this down to a smaller size. If I go into DB properties and enter a
new value it says that the size must be greater than the current size.
Is there any way to do this? Also when I try and restore the DB is
wants 20GB of disk space even though it's only using about 1GB of
data. Any help will be muchly appreciated.
Darren.
Use DBCC SHRINKDATABASE or DBCC SHRINKFILE. These commands are documented in Books Online.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Darren" <walker_712@.hotmail.com> wrote in message news:cd9d3666.0405102239.21563966@.posting.google.c om...
> I created a DB with a preallocated size of 20GB and I need to shink
> this down to a smaller size. If I go into DB properties and enter a
> new value it says that the size must be greater than the current size.
> Is there any way to do this? Also when I try and restore the DB is
> wants 20GB of disk space even though it's only using about 1GB of
> data. Any help will be muchly appreciated.
> Darren.

Resizing an existing DB

I created a DB with a preallocated size of 20GB and I need to shink
this down to a smaller size. If I go into DB properties and enter a
new value it says that the size must be greater than the current size.
Is there any way to do this? Also when I try and restore the DB is
wants 20GB of disk space even though it's only using about 1GB of
data. Any help will be muchly appreciated.
Darren.Use DBCC SHRINKDATABASE or DBCC SHRINKFILE. These commands are documented in
Books Online.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Darren" <walker_712@.hotmail.com> wrote in message news:cd9d3666.0405102239.21563966@.posting
.google.com...
> I created a DB with a preallocated size of 20GB and I need to shink
> this down to a smaller size. If I go into DB properties and enter a
> new value it says that the size must be greater than the current size.
> Is there any way to do this? Also when I try and restore the DB is
> wants 20GB of disk space even though it's only using about 1GB of
> data. Any help will be muchly appreciated.
> Darren.

Resetting the Identity field

I have a composite pk in a table 'table' in ms sql server. value in one field 'table.a' is fk to another table 'table1.a'
value in field table.b is a id field. i need to reset this field 'table.b' to 1 each time the 'table.a' changes.

Any suggestions.Not sure what you mean.
if table.a is part of the primary key it should never change otherwise it shouldn't be part of the primary key.
It sounds like you might want a trigger but maybe you could post an example.|||i see what you mean. I have changed it and i have a field table1.a and table1.b. both .a and .b are not in the keys, though .a is a fk to table2.a.
i need to increment .b by 1 on each input of .a where .a = 'x' (say). as soon as .a = 'y' (say) i need to reset .b to 0 and auto increment as new values for .a='y' are inserted.
hope this makes sense.
thanks|||ok
you have table1(a,b)
a is an id and you want b to be the sequence number within a?

put a trigger on the table

create trigger tr_table1_ins on table1 for insert
as
set rowcount 1
while exists(select * from table1 where b is null)
begin
update table1
set b = (select max(b)+1 from table1 t1 where table1.a = t1.a)
where b is null
set rowcount 0
go

if you only ever insert one row at a time then you can just do the update without the loop.

Another option is to put the current value for b on table2 and increment it within a transaction on inserts and use it with the insert.|||We've done something like this at our site. When we needed to know the occurence of a record, example "2 of 5". We implemented a TRIGGER like nigelrivett has suggested. To use a trigger you should JOIN with the INSERTED table to update only those records that were Inserted.

SET NOCOUNT ON
GO
CREATE
TABLE Occurrence
(
syID int IDENTITY (1, 1) NOT NULL ,
colA char(3),
colB int NOT NULL DEFAULT 0
)
GO
CREATE
TRIGGER tri_Occurrence
ON Occurrence
FOR Insert
AS

--
-- If no records were effected then return
--
IF (@.@.ROWCOUNT = 0) BEGIN
RETURN
END

UPDATE o
SET colB = (SELECT MAX(o.colB) + 1 FROM Occurrence o WHERE i.colA = o.colA)
FROM Occurrence o,
Inserted i
WHERE o.syID = i.syID

RETURN
GO

INSERT Occurrence (colA) values ('A')
INSERT Occurrence (colA) values ('A')
INSERT Occurrence (colA) values ('B')
INSERT Occurrence (colA) values ('A')
INSERT Occurrence (colA) values ('C')
INSERT Occurrence (colA) values ('C')
GO

SELECT *
FROM Occurrence

syID colA colB
---- -- ----
1 A 1
2 A 2
3 B 1
4 A 3
5 C 1
6 C 2|||Unfortunately that only works for single row inserts.
And assumes an ID on the table.
Apart from that is the same as my trigger.|||You are one to get the last word in. I'm sorry that I replied to the posting with my answer. I felt that a person could cut and paste this and see a working example.

But I forget that once nigelrivett answers, we should lock the posting, case closed.|||Sorry - just thought I'd point out a problem, which is quite common, with the trigger you posted.

Monday, March 26, 2012

Reseting Identity Seed

hi all..
I want to reset the identity seed value for a table... How can I do that...
one method is to truncate table... but if table is used as parent in foreign
key relationship; it does not allow to truncate the table... any other
method?
Ansari
Check out DBCC CHECKIDENT.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Ansari" <mudasar_ansari@.yahoo.com> wrote in message news:uf4BRq4AFHA.4028@.TK2MSFTNGP15.phx.gbl...
> hi all..
>
> I want to reset the identity seed value for a table... How can I do that...
> one method is to truncate table... but if table is used as parent in foreign
> key relationship; it does not allow to truncate the table... any other
> method?
> Ansari
>
>

Reseting Identity Seed

hi all..
I want to reset the identity seed value for a table... How can I do that...
one method is to truncate table... but if table is used as parent in foreign
key relationship; it does not allow to truncate the table... any other
method?
AnsariCheck out DBCC CHECKIDENT.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Ansari" <mudasar_ansari@.yahoo.com> wrote in message news:uf4BRq4AFHA.4028@.TK2MSFTNGP15.phx.
gbl...
> hi all..
>
> I want to reset the identity seed value for a table... How can I do that..
.
> one method is to truncate table... but if table is used as parent in forei
gn
> key relationship; it does not allow to truncate the table... any other
> method?
> Ansari
>
>

Reseting Identity Seed

hi all..
I want to reset the identity seed value for a table... How can I do that...
one method is to truncate table... but if table is used as parent in foreign
key relationship; it does not allow to truncate the table... any other
method?
AnsariCheck out DBCC CHECKIDENT.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Ansari" <mudasar_ansari@.yahoo.com> wrote in message news:uf4BRq4AFHA.4028@.TK2MSFTNGP15.phx.gbl...
> hi all..
>
> I want to reset the identity seed value for a table... How can I do that...
> one method is to truncate table... but if table is used as parent in foreign
> key relationship; it does not allow to truncate the table... any other
> method?
> Ansari
>
>

reseting 'allow new subscriptions to be created by attaching..."

1st question: can this value be changed from 'false' to 'true' after a
publication is up and running?
2nd question: can this be accomplished without re-initializing the
subscribers?
3rd question: if #1 & #2 are true what steps must be done to do this?
Note: I have already tried the sp_changeMergePublication sp with no
success:
exec sp_changeMergePublication
@.publication = 'rep_test_hub',
@.property = allow_subscription_copy,
@.value = true
Thanks.
1) I am able to do it with a pull subscription.
2) Yes
3) I ran the same command to allow this.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"astro" <astro@.bcmn.com> wrote in message
news:N38Bf.42034$7S.25014@.tornado.rdc-kc.rr.com...
> 1st question: can this value be changed from 'false' to 'true' after a
> publication is up and running?
> 2nd question: can this be accomplished without re-initializing the
> subscribers?
> 3rd question: if #1 & #2 are true what steps must be done to do this?
> Note: I have already tried the sp_changeMergePublication sp with no
> success:
> exec sp_changeMergePublication
> @.publication = 'rep_test_hub',
> @.property = allow_subscription_copy,
> @.value = true
>
> Thanks.
>
|||hummm...time to turn trace on and check the logs i guess...
"Hilary Cotter" <hilary.cotter@.gmail.com> wrote in message
news:%230rE$5IIGHA.1124@.TK2MSFTNGP10.phx.gbl...
> 1) I am able to do it with a pull subscription.
> 2) Yes
> 3) I ran the same command to allow this.
> --
> Hilary Cotter
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
> "astro" <astro@.bcmn.com> wrote in message
> news:N38Bf.42034$7S.25014@.tornado.rdc-kc.rr.com...
>
|||BTW - you know that after you have enabled this setting, you have to pull
your subscription to another database using an anonymous pull, and then do
the copy subscription database from this subscriber database, right?
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"astro" <astro@.bcmn.com> wrote in message
news:OlqBf.42145$7S.2470@.tornado.rdc-kc.rr.com...
> hummm...time to turn trace on and check the logs i guess...
> "Hilary Cotter" <hilary.cotter@.gmail.com> wrote in message
> news:%230rE$5IIGHA.1124@.TK2MSFTNGP10.phx.gbl...
>

Friday, March 23, 2012

Reset Next Range Starting Value?

Hello,
Is there any way to reset the "Next range starting value" on a table
with Identity Range management?
For some reason, the next range starting value has gone too high. The
maximum identity is 32767, and the next range starting value is 32650.
I did have the range size at subscribers set to 200, but there wasn't
enough room to give that range so the initial replication failed. I
changed it to 50 in order to get the initial subscription pulling.
The actual maximum identity in the table is less than 2000. I only
have 2 subscribers.
I'm not sure how it got so high, but is there a way to reset it?
Thanks,
Jeff
You can manually update the msrepl_identity (sp?) table in the distribution
database.
However, you should poll your subscribers and publisher to find out what is
in effect there.
Check the check constraint on the identity key and do a DBCC
checkindent('tablename') to get the current value and the value in use.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Jeff Hedlund" <jhedlund@.gmail.com> wrote in message
news:1116082608.807565.267540@.o13g2000cwo.googlegr oups.com...
> Hello,
> Is there any way to reset the "Next range starting value" on a table
> with Identity Range management?
> For some reason, the next range starting value has gone too high. The
> maximum identity is 32767, and the next range starting value is 32650.
> I did have the range size at subscribers set to 200, but there wasn't
> enough room to give that range so the initial replication failed. I
> changed it to 50 in order to get the initial subscription pulling.
> The actual maximum identity in the table is less than 2000. I only
> have 2 subscribers.
> I'm not sure how it got so high, but is there a way to reset it?
> Thanks,
> Jeff
>

reset next identity value

For a set of tables many of which contain FK constraints related to identity
cols in the foreign tables:
When deleting all data (dropping FK constraints first) and then repopulating
data from scripts , how to instruct SS to reset the next identity value to
the identity seed ?TRUNCATE TABLE, or check out DBCC CHECKIDENT (TableName, RESEED)
Roy
On Mon, 27 Feb 2006 17:14:18 -0800, "John A Grandy"
<johnagrandy-at-yahoo-dot-com> wrote:

>For a set of tables many of which contain FK constraints related to identit
y
>cols in the foreign tables:
>When deleting all data (dropping FK constraints first) and then repopulatin
g
>data from scripts , how to instruct SS to reset the next identity value to
>the identity seed ?

Wednesday, March 21, 2012

Reset Autonumber Value in MS SQL 2k

I am learning the differences between Access and SQL the hard way.

SQL will not simply allow me, through an append query. to reset the starting value of my issueing ID field (autonumber). The new value is always higher than the last sequenced range.

I do not have experience with triggers, and from what I am reading this may be my solution.

Basically, I have a reference table that issues ID's when an ID is needed (table B). I only know the current range (min and max) numbers. Once this range is completely allocated. A new range of numbers is entered. New ranges can be 700 IDs or 7000 IDs.. never know until they are issued from other source.

How to reset the value for my current autonumber (increment) field in SQL.I am assuming that you are referring to an identity column, if so have a read about: DBCC CHECKIDENT in BOL.

Hope that helps.

Originally posted by jctrips
I am learning the differences between Access and SQL the hard way.

SQL will not simply allow me, through an append query. to reset the starting value of my issueing ID field (autonumber). The new value is always higher than the last sequenced range.

I do not have experience with triggers, and from what I am reading this may be my solution.

Basically, I have a reference table that issues ID's when an ID is needed (table B). I only know the current range (min and max) numbers. Once this range is completely allocated. A new range of numbers is entered. New ranges can be 700 IDs or 7000 IDs.. never know until they are issued from other source.

How to reset the value for my current autonumber (increment) field in SQL.|||THis definitely helps for identifying the seed. And I can see how to RESEED.

But how now to automate this based on new Range inputted by Admin User on Front End?|||DBCC CHECKIDENT ('table_name', RESEED, new_reseed_value) can be specified as per the parameters you accept.

Reseeding to a value that exists

Hi group,
If I have a table, SomeTable like so:
(SQL Server 2000)
[ID] [int] IDENTITY (1, 1) NOT NULL
[SomeValue] [varchar] (50)
ID SomeValue
1 something
2 something other
3 something else
And I then execute:
DBCC CHECKIDENT (SomeTable, RESEED, 2)
And I then execute:
INSERT INTO SomeTable VALUES('something more')
This will leave me with two values with the same ID of 3. What I expected
was that I'd receive an error when trying to reseed at 2, when 2 already
exists, but this is not the case. Is this a DB option that I do not have
set that allows duplicate identities, and/or is there something that I can
change so that if I am ever stupid enough to reseed at an existing value,
that I will be denied with an error?
Thank you,
Ray at workAny of the following will guarantee uniqueness:
1) primary key constraint
2) unique constraint
3) unique index
--
Hope this helps.
Dan Guzman
SQL Server MVP
--
SQL FAQ links (courtesy Neil Pike):
http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
--
"Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
news:%23Z7Db0gqDHA.1444@.tk2msftngp13.phx.gbl...
> Hi group,
> If I have a table, SomeTable like so:
> (SQL Server 2000)
> [ID] [int] IDENTITY (1, 1) NOT NULL
> [SomeValue] [varchar] (50)
> ID SomeValue
> 1 something
> 2 something other
> 3 something else
> And I then execute:
> DBCC CHECKIDENT (SomeTable, RESEED, 2)
> And I then execute:
> INSERT INTO SomeTable VALUES('something more')
> This will leave me with two values with the same ID of 3. What I
expected
> was that I'd receive an error when trying to reseed at 2, when 2
already
> exists, but this is not the case. Is this a DB option that I do not
have
> set that allows duplicate identities, and/or is there something that I
can
> change so that if I am ever stupid enough to reseed at an existing
value,
> that I will be denied with an error?
> Thank you,
> Ray at work
>
>

RESEEDING & INCREMENT VALUES

hi,
I am maintaining around 180 tables, out of these-125 have identity values
I need set new SEED value and new increment values for these 125 tables
Is there any easier way to do this?
Thanks,
Soura
you can do something like this
SELECT 'DBCC CHECKIDENT ('+TABLE_NAME +',RESEED , 50 )'
FROM INFORMATION_SCHEMA.TABLES
WHERE IDENT_INCR(TABLE_NAME) IS NOT NULL
This will reseed the next identity value to 50.
I'm not sure how to change the increment.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
"SouRa" <SouRa@.discussions.microsoft.com> wrote in message
news:352CE298-4960-43AB-8C9B-A098FF4CAB6F@.microsoft.com...
> hi,
> I am maintaining around 180 tables, out of these-125 have identity
values
> I need set new SEED value and new increment values for these 125 tables
> Is there any easier way to do this?
> Thanks,
> Soura

Reseed identity column in SQL 2000

Hello. I see that we're not permitted to reseed a incremental column to a
value that's lower than the highest value in the table. I guess that's in
there for our protection but is there any way around this? On an integer
column I manually set row to a value of 2 billion and had planned to reseed
to a very low number never to approach it. If we have to start at 2 billion
it's possible that we'll run out of room.
Guess I can change the column to a large int. Is anyone thinking that
identity fields are more hassle than they're worth?
TIA,
Ken Trockktrock wrote:
> Hello. I see that we're not permitted to reseed a incremental column to a
> value that's lower than the highest value in the table. I guess that's in
> there for our protection but is there any way around this? On an integer
> column I manually set row to a value of 2 billion and had planned to resee
d
> to a very low number never to approach it. If we have to start at 2 billio
n
> it's possible that we'll run out of room.
> Guess I can change the column to a large int. Is anyone thinking that
> identity fields are more hassle than they're worth?
> TIA,
> Ken Trock
I think you are referring to this comment in Books Online:
"If the value of new_reseed_value is less than the maximum value in the
identity column, error message 2627 will be generated on subsequent
references to the table."
http://msdn.microsoft.com/library/d...asp?frame=true
This implies that setting a value lower than the maximum won't work but
if you know that error message 2627 is a key violation error you'll
realize that a problem can only occur when and if you violate a
constraint by generating a duplicate value. So in fact you can do
exactly what you have described.
I notice this is slightly clearer in 2005 BOL:
"If the value of new_reseed_value is less than the maximum value in the
identity column and a PRIMARY KEY or UNIQUE constraint exists on the
identity column, error message 2627 will be generated on subsequent
references to the table."

> Guess I can change the column to a large int. Is anyone thinking that
> identity fields are more hassle than they're worth?
Yes they are a gerat big hassle. A lot of the time IDENTITY columns are
woefully and needlessly misused and abused by people who don't even
realize what they are doing wrong.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||of course you can't do that.
why are you adding a row with this very high value? is it some kind of
stub row? why not make it 0 or go negative with it instead?
ktrock wrote:
> Hello. I see that we're not permitted to reseed a incremental column to a
> value that's lower than the highest value in the table. I guess that's in
> there for our protection but is there any way around this? On an integer
> column I manually set row to a value of 2 billion and had planned to resee
d
> to a very low number never to approach it. If we have to start at 2 billio
n
> it's possible that we'll run out of room.
> Guess I can change the column to a large int. Is anyone thinking that
> identity fields are more hassle than they're worth?
> TIA,
> Ken Trock|||if you really want to do that, move all the rows with idenities higher
than the value to another table, set the seed to the value, and move
the rows back with IDENTITY_INSERT on|||holy crap - i always thought this was a silly enough thing to be want to
do, but to actually be able to do it...
Ken - ok - i was wrong, you can do it. but i still say you shouldn't
(and shouldn't be able to...)
David Portas wrote:
> ktrock wrote:
>
>
> I think you are referring to this comment in Books Online:
> "If the value of new_reseed_value is less than the maximum value in the
> identity column, error message 2627 will be generated on subsequent
> references to the table."
> http://msdn.microsoft.com/library/d...asp?frame=true
> This implies that setting a value lower than the maximum won't work but
> if you know that error message 2627 is a key violation error you'll
> realize that a problem can only occur when and if you violate a
> constraint by generating a duplicate value. So in fact you can do
> exactly what you have described.
> I notice this is slightly clearer in 2005 BOL:
> "If the value of new_reseed_value is less than the maximum value in the
> identity column and a PRIMARY KEY or UNIQUE constraint exists on the
> identity column, error message 2627 will be generated on subsequent
> references to the table."
>
>
>
> Yes they are a gerat big hassle. A lot of the time IDENTITY columns are
> woefully and needlessly misused and abused by people who don't even
> realize what they are doing wrong.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>|||Thanks all for the replies. Will try 1 of these options.
Ken
"David Portas" wrote:

> ktrock wrote:
> I think you are referring to this comment in Books Online:
> "If the value of new_reseed_value is less than the maximum value in the
> identity column, error message 2627 will be generated on subsequent
> references to the table."
> http://msdn.microsoft.com/library/d...asp?frame=true
> This implies that setting a value lower than the maximum won't work but
> if you know that error message 2627 is a key violation error you'll
> realize that a problem can only occur when and if you violate a
> constraint by generating a duplicate value. So in fact you can do
> exactly what you have described.
> I notice this is slightly clearer in 2005 BOL:
> "If the value of new_reseed_value is less than the maximum value in the
> identity column and a PRIMARY KEY or UNIQUE constraint exists on the
> identity column, error message 2627 will be generated on subsequent
> references to the table."
>
> Yes they are a gerat big hassle. A lot of the time IDENTITY columns are
> woefully and needlessly misused and abused by people who don't even
> realize what they are doing wrong.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>sql

Monday, March 12, 2012

Require help writing a function

Hi there
I am trying to write a function which will return the Maximum value in a
field of a given table and have run into problems.
I want to pass the name of the field and the name of the table to the
function, have it find the Max value and then add 1 to that number and
return the result.
After doing some reading I thought a Simple Scalar Function would be the way
to go but I just cannot make it work.
Could anyone help me please.
Thanks
June
hi June,
"June Macleod" <junework@.hotmail.com> ha scritto nel messaggio
news:esYBukT4EHA.2568@.TK2MSFTNGP10.phx.gbl
> Hi there
> I am trying to write a function which will return the Maximum value
> in a field of a given table and have run into problems.
> I want to pass the name of the field and the name of the table to the
> function, have it find the Max value and then add 1 to that number and
> return the result.
> After doing some reading I thought a Simple Scalar Function would be
> the way to go but I just cannot make it work.
>
what you want to do is known as Dynamic SQL, which provides poor
performances and security holes toubles...
for your informatgion, please have a look at
http://www.sommarskog.se/dynamic_sql.html for a great explanation of it's
uses and related troubles... and something like
CREATE FUNCTION dbo.udfNextValue (
@.col VARCHAR(10) ,
@.tbl VARCHAR (10)
) RETURNS INT
AS BEGIN
DECLARE @.cmd NVARCHAR
DECLARE @.Value INT
SET @.cmd ='SELECT @.Value = ISNULL(MAX(' + @.col + ') , 0) + 1 FROM ' + @.tbl
EXEC sp_executesql @.cmd, N'@.Value INT OUTPUT', @.Value OUTPUT
RETURN (@.Value)
END
is unfortunately not permitted within user defined function...
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.9.1 - DbaMgr ver 0.55.1
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply

Requesting null value

if

(paramNum1_cel.Value ==null)this.gerentes_beneficios.numCelular = // what do i put hereelsethis.gerentes_beneficios.numCelular = (int) result ["Num1_cel"];

i need something to define a resultset, this field can be null or not

thanks a lot

I don't understand your question. Could you explain it further?

Friday, March 9, 2012

Requery Parameter Fields

I have a report that runs on a number of parameter date fields (coverted to string). At present, the report automatically defaults to MAX value of a date. The syntax for that particular element/dataset is

SELECT CONVERT(char(11), MAX(src_date)) AS src_date_cur
FROM tbl_src_date

What I am looking to acheive is that if I was to modify the parameter to allow the user to specifiy a date from that particular field (rather than default to MAX), how could I get the subsequent parameter boxes to requery automatically. The code I use for the other 5 parameter fields is;

SELECT CONVERT(char(11), MAX(src_date)) AS src_date_cur
, CONVERT(char(11), dateadd(mm,-1, MAX(src_date))) AS src_date_pre
FROM tbl_src_date

SELECT CONVERT(char(11), MAX(src_date)) AS src_date_cur
, CONVERT(char(11), dateadd(mm,-2, MAX(src_date))) AS src_date_pre2
FROM tbl_src_date

SELECT CONVERT(char(11), MAX(src_date)) AS src_date_cur
, CONVERT(char(11), dateadd(mm,-3, MAX(src_date))) AS src_date_pre3
FROM tbl_src_date

SELECT CONVERT(char(11), MAX(src_date)) AS src_date_cur
, CONVERT(char(11), dateadd(mm,-4, MAX(src_date))) AS src_date_pre4
FROM tbl_src_date

SELECT CONVERT(char(11), MAX(src_date)) AS src_date_cur
, CONVERT(char(11), dateadd(mm,-5, MAX(src_date))) AS src_date_pre5
FROM tbl_src_date

If it makes a difference I am using SS-RS 2005.

Regards

Make them source from a dataset which has a dependency on the Parameter. This will create a hierarchy, whereas the first parameter value (e.g. a datetime picker has to be selected first)

HTH, Jens Suessmeyer.


http://www.sqlserver2005.de

Wednesday, March 7, 2012

Represent a value list as a table for Outer Join??

This might not be possible, but on the chance that it can - is there a
way to do the following:

Given a arbitray one dimesional value list:
('AALGX','12345','XXXXX','AAINX','AMMXX')

Is there a way that I could do a select statement, or similiar, in the
value list, to get the following result

field_name
----
AALGX
12345
XXXXX
AAINX
AMMXX

Because, what I want to be able to do in the long run is essentially
perform an outer join on the value list.

Something along the lines of

select value_list.field_name, dbtable.otherfield FROM value_list left
outer join dbtable on value_list.field_name = dbtable.field_name

So I want all the values in the field list to show up, and any
matching data in the database table that exists, otherwise null.

Maybe there is another approach to this?

Thanks!
KTKT,

If the value list is static, you could just do

SELECT field_name, otherfield
FROM dbtable
WHERE field_name IN ('AALGX','12345','XXXXX','AAINX','AMMXX')

I doubt that's the case though, so take a look at
http://www.aspfaq.com/show.asp?id=2248

-Andy

"KT" <kristin@.masterypoint.com> wrote in message
news:f1bdb6c4.0409241036.332ec125@.posting.google.c om...
> This might not be possible, but on the chance that it can - is there a
> way to do the following:
> Given a arbitray one dimesional value list:
> ('AALGX','12345','XXXXX','AAINX','AMMXX')
> Is there a way that I could do a select statement, or similiar, in the
> value list, to get the following result
> field_name
> ----
> AALGX
> 12345
> XXXXX
> AAINX
> AMMXX
> Because, what I want to be able to do in the long run is essentially
> perform an outer join on the value list.
> Something along the lines of
> select value_list.field_name, dbtable.otherfield FROM value_list left
> outer join dbtable on value_list.field_name = dbtable.field_name
> So I want all the values in the field list to show up, and any
> matching data in the database table that exists, otherwise null.
> Maybe there is another approach to this?
> Thanks!
> KT|||Right - that is the way to do it as an inner join, getting only the
records that match, but I want to use a value list in an outer join - is
that possible??

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||Andy -

Actually, I was able to use that link you sent along to accomplish the
outer join part of the goal! Thanks - that was really helpful!!!
KT

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||> Given a arbitray one dimesional value list:
> ('AALGX','12345','XXXXX','AAINX','AMMXX')
> Is there a way that I could do a select statement, or similiar, in the
> value list, to get the following result
> field_name
> ----
> AALGX
> 12345
> XXXXX
> AAINX
> AMMXX

Hi KT,

Try the following:

SELECT *
FROM (
SELECT 'AALGX'
UNION ALL
SELECT '12345'
UNION ALL
SELECT 'XXXXX'
UNION ALL
SELECT 'AAINX'
UNION ALL
SELECT 'AMMXX'
) AS value_list (column_name)

So your outer join would look something like:

SELECT *
FROM other_table o
LEFT OUTER JOIN
(
SELECT 'AALGX'
UNION ALL
SELECT '12345'
UNION ALL
SELECT 'XXXXX'
UNION ALL
SELECT 'AAINX'
UNION ALL
SELECT 'AMMXX'
) AS value_list (column_name)
ON o.column_name = value_list.column_name

Christian.