Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

Friday, March 23, 2012

Need SQL Help - Trim parentheses in column of data

Hi All, I am sure someone has done this before. Trying to write a SQL statement to UPDATE a column named Prod_Model. The table name is tbl_MASTER. Wanting to trim the parentheses out of the data and update back to the table. Any help on the SQL statement?

Trim or remove? Trim usually means only leading and/or trailing, and remove means to get rid of all.

Here is how to remove:

UPDATE Table

SET field=REPLACE(REPLACE(field,')',''),'(','')

WHERE field LIKE '%)%' OR field LIKE '%(%'

Here is how to trim:

UPDATE Table

SET field=REVERSE(SUBSTRING(REVERSE(SUBSTRING(field,PATINDEX('%[^()]%',field),LEN(field)-PATINDEX('%[^()]%',field)+1)),PATINDEX('%[^()]%',REVERSE(SUBSTRING(field,PATINDEX('%[^()]%',field),LEN(field)-PATINDEX('%[^()]%',field)+1))),LEN(REVERSE(SUBSTRING(field,PATINDEX('%[^()]%',field),LEN(field)-PATINDEX('%[^()]%',field)+1)))-PATINDEX('%[^()]%',REVERSE(SUBSTRING(field,PATINDEX('%[^()]%',field),LEN(field)-PATINDEX('%[^()]%',field)+1)))+1))

WHERE field LIKE '(%' or field LIKE ')%' or field LIKE '%)' or field LIKE '%('

There is a much shorter way using STUFF, REVERSE, and PATINDEX, but I thought of this way first.

|||

The remove method did the ticket. Thank you so much.

sql

Wednesday, March 21, 2012

Need some help with update query

Hi There ,

I run the query below and get the error message below the query.
How can i do this update?

UPDATE GEO_Plaats
SET NetNummer = (SELECT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats)
WHERE EXISTS
(SELECT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats);

ERROR:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

Cheers WimmoThis is untested but it might be of assistance.

UPDATE GEO_Plaats
SET NetNummer = n.Netnummer
FROM GEO_Plaats p
JOIN GEO_Netnummers n
ON p.Plaats = n.Plaats

Good Luck!|||As you see problem is in your subquery

(SELECT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats)

which returns more than 1 record from GEO_Netnummers table for specific Plaats in GEO_Plaats table. Simply speaking one "Plaat" from GEO_Plaats have more records in GEO_Netnummers table (cardinality is 1:N)

If all records in GEO_Netnummers have the same Netnummer for specific GEO_Plaats.Plaats you can use distinct to retrive 1 record:

UPDATE GEO_Plaats
SET NetNummer = (SELECT DISTINCT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats)
WHERE EXISTS
(SELECT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats);

If GEO_Netnummers.Netnummer differs for specific GEO_Plaats.Plaats you have 2 options:
1) use max() or min() function to retrieve 1 record. But in this case you have to realise if your subquery returns Netnummers: 1, 2, 5, 70 you select max() it means 70 but what if 5 is correct one?

UPDATE GEO_Plaats
SET NetNummer = (SELECT max(GEO_Netnummers.Netnummer)
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats)
WHERE EXISTS
(SELECT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats);

2) use another restrictions in your WHERE clause to retrieve 1 unique number. This restrictions depend on your "business'' rules

UPDATE GEO_Plaats
SET NetNummer = (SELECT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats
AND GEO_Netnummers.SOMETHING = SOMETHING
...)
WHERE EXISTS
(SELECT GEO_Netnummers.Netnummer
FROM GEO_Netnummers
WHERE GEO_Netnummers.Plaats = GEO_Plaats.Plaats);

It seems to me, Netnummer is primary key in GEO_Netnummers table, so I think DISTINC won't help. In this case I'd use 2. scenario and option 2|||As you see problem is in your subquery
[...]
which returns more than 1 record from GEO_Netnummers table for specific Plaats in GEO_Plaats table. Simply speaking one "Plaat" from GEO_Plaats have more records in GEO_Netnummers table (cardinality is 1:N)

It might be a data problem. The table and column names indicate this is about the Dutch phone system; plaats = city, netnummer = area code.

As far as I know, a 'plaats' is supposed to have exactly 1 'netnummer'; several 'plaatsen' may share a 'netnummer'. If that's true and this error occurs, there may be invalid data in the GEO_netnummers table.

The error might also occur if the joining is done on the city's name; several cities may exist with identical names but different 'netnummers'.

Need some help with DTS

I need to set up an SQL datbase table to automaticaly update its data from another database (outside of the SQL server). I am not extreemly familiar with SQL server and DTS, so im not sure how to go about this.

What i need to do is;

1. select all records in the destination database that no longer have matches in the source database and set a column in the destination databvase (one not in the source table) to a value, such as "not active". So if the record is deleted in the source database, its only marked as deleted in the destination database.

2. Update columns in the destination database with changes made to the source.

3. Add any new records to the destination database that have been added to the source database.

4. Have this run once a day.

I have done little research and the parts 2 and 3 dont seem like it would be hard to figure out, but the first part I cant seem to find anything on how do get it accomplished.

I am working in SQL Server 7. Any help for this noob to DTS would be appreciate, especialy articles on how to accomplish set 1.Sounds like a case for replication database but don't know if it works on linked database.
Think you can do it with Views, maybe called by DTS

First link source database so you can use it's Alias in views (SourceServer)
Do this in Security\linked servers in SQL Server Enterprice Manager

Make VIEW1 in destination dbase:

SELECT DestinationTable.Record_Id
FROM DestinationTable LEFT JOIN SourceServer.SourceDatabase.Dbo.SourceTable AS ST ON DestinationTable.Record_Id = ST.Record_Id
WHERE (((ST.Record_Id ) Is Null));

Make VIEW2 that uses results from VIEW1:

UPDATE DestinationTable INNER JOIN VIEW1 ON DestinationTable.Record_Id = VIEW1.Record_Id SET DestinationTable.RemarkColumn = "deleted"|||Tried making that View1 lke you said, but its returning all rows in the destination table, not jus the ones that dont have matches in the source table|||Make sure the join is LEFT JOIN. Because of criteria in source (IS NULL) it should only return the ones you want.

Use CAPITAL (IS NULL) in criteria

good luck|||I got that working, thanks.

New problem. SQL server will not let me save a view with an update command in it.

I was thinking i could just use the SQL statement and have it to run once a day. Is there a way to schedule SQL statements to run peridicaly in DTS?sql

Need some help in query

Is their best to to execute update query. here is my problem
I have a two table on different databases on sql server 2000...
one called source which has 104 fields and another called des table with
same fields...
i want to check if any thing changed in source update the destination....
now i create a sql string mean uinsg dynamic sql to create where clause cos
if write a query my self it will take ages to write that where clause cos i
am comparing each field with each field using OR statment... then i cannt d
o
it with update so i delete the records from des table and insert again from
source table.... as writing update query need too mush string and also will
be problem to put where clause ....
is what i am doing is ok or any other better idea to overcome that problem
thanksHi,
You may use COLUMNS_UPDATED() function in the trigger to find out the
updated columns. But still you have the difficulty of transferring the
changes to dest table. Probably dynamically creating the update command can
help. If you want to get rid of writing any code, replication is an option!
"amjad" <amjad@.discussions.microsoft.com> wrote in message
news:8A6199E7-8F13-4BC3-BBAE-9DC56D699EE6@.microsoft.com...
> Is their best to to execute update query. here is my problem
> I have a two table on different databases on sql server 2000...
> one called source which has 104 fields and another called des table with
> same fields...
> i want to check if any thing changed in source update the destination....
> now i create a sql string mean uinsg dynamic sql to create where clause
> cos
> if write a query my self it will take ages to write that where clause cos
> i
> am comparing each field with each field using OR statment... then i cannt
> do
> it with update so i delete the records from des table and insert again
> from
> source table.... as writing update query need too mush string and also
> will
> be problem to put where clause ....
> is what i am doing is ok or any other better idea to overcome that problem
> thanks|||It would probably take less time to manually write out all 104 columns than
the time you have spent messing with the dynamic SQL. Maybe a few minutes
in any decent text editor?
"amjad" <amjad@.discussions.microsoft.com> wrote in message
news:8A6199E7-8F13-4BC3-BBAE-9DC56D699EE6@.microsoft.com...
> Is their best to to execute update query. here is my problem
> I have a two table on different databases on sql server 2000...
> one called source which has 104 fields and another called des table with
> same fields...
> i want to check if any thing changed in source update the destination....
> now i create a sql string mean uinsg dynamic sql to create where clause
cos
> if write a query my self it will take ages to write that where clause cos
i
> am comparing each field with each field using OR statment... then i cannt
do
> it with update so i delete the records from des table and insert again
from
> source table.... as writing update query need too mush string and also
will
> be problem to put where clause ....
> is what i am doing is ok or any other better idea to overcome that problem
> thanks

Monday, March 19, 2012

Need sample code for asp + sql server

Hello,
I am developing for internet.
I need a good example for ASP page, that use sql-server with some update
commands + transactions (use insert/update command + beginTrans/EndTrans,
please.
Thanks
Try a site like http://www.asp101.com/ or http://www.4guysfromrolla.com/
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)
"Avi" <nobody@.nospam_please.com> wrote in message
news:u8BcHvD8EHA.1292@.TK2MSFTNGP10.phx.gbl...
> Hello,
> I am developing for internet.
> I need a good example for ASP page, that use sql-server with some update
> commands + transactions (use insert/update command + beginTrans/EndTrans,
> please.
> Thanks
>
|||Have a look at
http://www.mindsdoor.net/aSP/DBAccess.inc.html

Need sample code for asp + sql server

Hello,
I am developing for internet.
I need a good example for ASP page, that use sql-server with some update
commands + transactions (use insert/update command + beginTrans/EndTrans,
please.
Thanks
Try a site like http://www.asp101.com/ or http://www.4guysfromrolla.com/
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)
"Avi" <nobody@.nospam_please.com> wrote in message
news:u8BcHvD8EHA.1292@.TK2MSFTNGP10.phx.gbl...
> Hello,
> I am developing for internet.
> I need a good example for ASP page, that use sql-server with some update
> commands + transactions (use insert/update command + beginTrans/EndTrans,
> please.
> Thanks
>
|||Have a look at
http://www.mindsdoor.net/aSP/DBAccess.inc.html

Need sample code for asp + sql server

Hello,
I am developing for internet.
I need a good example for ASP page, that use sql-server with some update
commands + transactions (use insert/update command + beginTrans/EndTrans,
please.
Thanks :)Try a site like http://www.asp101.com/ or http://www.4guysfromrolla.com/
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)
"Avi" <nobody@.nospam_please.com> wrote in message
news:u8BcHvD8EHA.1292@.TK2MSFTNGP10.phx.gbl...
> Hello,
> I am developing for internet.
> I need a good example for ASP page, that use sql-server with some update
> commands + transactions (use insert/update command + beginTrans/EndTrans,
> please.
> Thanks :)
>|||Have a look at
http://www.mindsdoor.net/aSP/DBAccess.inc.html

Need sample code for asp + sql server

Hello,
I am developing for internet.
I need a good example for ASP page, that use sql-server with some update
commands + transactions (use insert/update command + beginTrans/EndTrans,
please.
Thanks Try a site like http://www.asp101.com/ or http://www.4guysfromrolla.com/
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)
"Avi" <nobody@.nospam_please.com> wrote in message
news:u8BcHvD8EHA.1292@.TK2MSFTNGP10.phx.gbl...
> Hello,
> I am developing for internet.
> I need a good example for ASP page, that use sql-server with some update
> commands + transactions (use insert/update command + beginTrans/EndTrans,
> please.
> Thanks
>|||Have a look at
http://www.mindsdoor.net/aSP/DBAccess.inc.html

Monday, March 12, 2012

Need Query for update and insert in one go.

Hi All,
Please look at the following tables. I have Two tables
Data and TmpData with the following structure.

Data ( All int columns, ID column is Primary and identity)

Id UserID PrgID RoldID
322 1 1 2
323 1 2 2
324 1 3 2
325 2 1 2
326 2 2 2
327 2 3 2
328 3 1 2
329 3 2 2
330 3 3 2

TmpData
Id UserID PrgID RoldID
82 1 1 3
83 1 2 3
84 1 3 3
85 2 1 3
86 2 2 3
87 2 3 3
91 20 1 2
92 20 2 2
93 20 3 2
94 21 1 2
95 21 2 2
96 21 3 2

Now I need to run a query so that
Part 1: It updates existing RoleId columns ( Based on Userid,PrgID) in 'Data' Table with corresponding values from 'Data' table.
Part2 : It inserts new rows in 'TmpData' to 'Data' table.

I am done with Part1 using the simple update statement.

Update Data
Set Data.programroleid=tmp.ProgramRoleID
from TmpData tmp
where Data.userid=tmp.UserId
and Data.programId=tmp.ProgramId

This works fine for existing userids,programids in 'Data' and 'TmpData' tables. But I am struggling with inserting new rows into Data from 'TmpData' ( That exists only in 'tmpData' table). Based on above table structures how do I insert new data into 'Data' table from 'TmpData'.INSERT INTO Data(ID, UserID, PrgID, RoldID)
SELECT * FROM TmpData
WHERE ID not in (SELECT ID FROM TmpData Inner Join Data on TmpData.UserID=Data.UserID and TmpData.ProgID=Data.PrgID and TmpData.RoldID=Data.RoldID)|||ID is auto-generated identity value, and thus cannot be inserted into the target table (unless you specifically disable this for the transaction...).
I wouldn't expect the identity values to match between Data and TmpData anyway, so the "NOT IN" method is probably not appropriate. It is also not efficient. NOT EXISTS is faster than NOT IN.

INSERT INTO Data
(ID,
UserID,
PrgID,
RoldID)
SELECT ID,
UserID,
PrgID,
RoldID
FROM TmpData
WHERE NOT EXISTS
(SELECT *
FROM Data
WHERE Data.UserID = TmpData.UserID
and Data.PrgID = TmpData.PrgID
and Data.RoldID = TmpData.RoldID)

Friday, March 9, 2012

Need Opinions on Updating Large tables

I have an incomming table that is similar to the following...
locationNbr, SalesDate, SalesAmt, TranCode, SalesQty
I have to update a table that is formated as such...
locationNbr, historyType, Year, D001, D002, D003, D004... --> D365
I am running a join from the incoming table to the table I need to update to
determine what records I need to Insert.
Then I am using a VB app to create a batch of update statements (because I
couldnt figure out how to dynamically specify columns in T-sql) for those
whose key records already exist.
I would imagine this would run alot faster if I could figure out a way to do
this within an sproc.
Is there a good way that I can dynamically build a SQL Update statement
within an sproc?
And, as I am not extremely familiar with MSSQL, is there any other fast
update methods when you are updating large tables?
Thanks for any input.Jace wrote:
> I have an incomming table that is similar to the following...
> locationNbr, SalesDate, SalesAmt, TranCode, SalesQty
> I have to update a table that is formated as such...
> locationNbr, historyType, Year, D001, D002, D003, D004... --> D365
>
You can use dynamic SQL, but I'm not sure it's going to provide any
improvements over all the processing your going to have to do to move
that nicely normalized table into one that looks strangely denormalized.
What's the story behind the denormalized table? What about leap years?
They have 366 days.
Check out sp_executesql in BOL for more information about how to build
dynamic SQL statements from in T-SQL.
David Gugick
Imceda Software
www.imceda.com|||Well, actually it has 371 columns, but it only uses the last 7 col every 9
years. The reason for the denorm format is for speed in accessing the data
for a forecasting method. This system forecasts 100'000+ skus in several
thousand locations, and I am told that using this table vs a nomalized table
increases the speed a significant amount.
Thanks for your input. And um... What is BOL ? :)
"David Gugick" wrote:

> Jace wrote:
> You can use dynamic SQL, but I'm not sure it's going to provide any
> improvements over all the processing your going to have to do to move
> that nicely normalized table into one that looks strangely denormalized.
> What's the story behind the denormalized table? What about leap years?
> They have 366 days.
> Check out sp_executesql in BOL for more information about how to build
> dynamic SQL statements from in T-SQL.
>
> --
> David Gugick
> Imceda Software
> www.imceda.com
>|||BOL is Books Online - the help documentation that comes with SQL Server.
The denormalized table design is potty and will not scale. Analysis Services
and indexed views are tools designed for this kind of work and can routinely
handle 100s of millions of rows of data.
David Portas
SQL Server MVP
--|||Jace wrote:
> Well, actually it has 371 columns, but it only uses the last 7 col
> every 9 years. The reason for the denorm format is for speed in
> accessing the data for a forecasting method. This system forecasts
> 100'000+ skus in several thousand locations, and I am told that using
> this table vs a nomalized table increases the speed a significant
> amount.
> Thanks for your input. And um... What is BOL ? :)
>
David is right. While denormailzed tables have been used for years in
read-only formats for reporting, it makes sense here to test a properly
designed, nomalized solution before commiting to this one.
David Gugick
Imceda Software
www.imceda.com|||Thanks for the input. Im going to do some testing on this in a denorm table
.
It appears the row count will be around 6.5 billion. Do you think that I
may run into trouble with that many records? Or will I have to split
different history types into different tables?
"David Portas" wrote:

> BOL is Books Online - the help documentation that comes with SQL Server.
> The denormalized table design is potty and will not scale. Analysis Servic
es
> and indexed views are tools designed for this kind of work and can routine
ly
> handle 100s of millions of rows of data.
> --
> David Portas
> SQL Server MVP
> --
>|||You'll almost certainly want to look at Analysis Services and Partitioned
Views for this.
Billions of rows are not uncommon - with the right hardware there is no
problem in principle with tables of that size. You should probably consult o
r
hire someone with AS and VLDB experience though.
David Portas
SQL Server MVP
--

Wednesday, March 7, 2012

Need Information about Processing and Aggregations (Update / Referenced Dimensions / Many to Man

HI!

I am looking for some information about processing of complex big models with partitioning.

facts:

we need a lot of aggreations have several dimension with 2 mio. members have partitioned the cube and want to process only a few and not all partitions want to process dimension by update (does not unporcess old partitions) or increment update have some dimensions with references relationship to the partitions may have linked measuregroups

questions:

update dimensions processing does this unprocess a cube or partition? we have the problem that our partitions get unprocessed? maybe if its referenced by ohter dimension or it is an parent child dimensions are there any circumstances where an update dimension unprocess the cube? how well are aggregations if we process update dimension how well are aggregations if we process update referenced dimension howto aggregate with a many to many measuregroup and update dimension

Links to any good documentation would be really interesting.

THANKS

HANNES

Some good material about AS2005 processing model: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sql2k5_asprocarch.asp

First. In AS2005 you have 2 types of processing that update dimension.

One is ProcessUpdate and another one is ProcessAdd.

update dimensions processing|||

Hi Hannes,

Did you find what was the problem that your partitions become unprocessed?

I have the same problem, i'm doing processing to new partitions and the old ones become 'Unprocessed'

Thanks in advance.

|||

It was because this was a test environment and we changed metadata. Added some attributes. This always need to be a full process.

LG, HANNES

|||

thanks for your answer.

I'm doing Process Full to the partitions, and I have the problem above.

the source database was changed but my query that reads the data hasn't changed, means that i have new columns in the fact table and i haven't add it to my query, does it matter? , i didn't changed my measure group at all.

Do you have any suggestion?

Do I need to do ProcessFull to dimensions as well?

thanks.

|||

If you do a full process of a partition the processing state of a cube does not change

(if the cube was prcessed before if is processed after

if the cube was not processed before it is not processed after - als log as you process by the xmla commands!)

Hannes

Need Information about Processing and Aggregations (Update / Referenced Dimensions / Many to Man

HI!

I am looking for some information about processing of complex big models with partitioning.

facts:

we need a lot of aggreations

have several dimension with 2 mio. members

have partitioned the cube and want to process only a few and not all partitions

want to process dimension by update (does not unporcess old partitions) or increment update

have some dimensions with references relationship to the partitions

may have linked measuregroups

questions:

update dimensions processing

does this unprocess a cube or partition?

we have the problem that our partitions get unprocessed?

maybe if its referenced by ohter dimension

or it is an parent child dimensions

are there any circumstances where an update dimension unprocess the cube?

how well are aggregations if we process update dimension

how well are aggregations if we process update referenced dimension

howto aggregate with a many to many measuregroup and update dimension

Links to any good documentation would be really interesting.

THANKS

HANNES

Some good material about AS2005 processing model: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sql2k5_asprocarch.asp

First. In AS2005 you have 2 types of processing that update dimension.

One is ProcessUpdate and another one is ProcessAdd.

update dimensions processing|||

Hi Hannes,

Did you find what was the problem that your partitions become unprocessed?

I have the same problem, i'm doing processing to new partitions and the old ones become 'Unprocessed'

Thanks in advance.

|||

It was because this was a test environment and we changed metadata. Added some attributes. This always need to be a full process.

LG, HANNES

|||

thanks for your answer.

I'm doing Process Full to the partitions, and I have the problem above.

the source database was changed but my query that reads the data hasn't changed, means that i have new columns in the fact table and i haven't add it to my query, does it matter? , i didn't changed my measure group at all.

Do you have any suggestion?

Do I need to do ProcessFull to dimensions as well?

thanks.

|||

If you do a full process of a partition the processing state of a cube does not change

(if the cube was prcessed before if is processed after

if the cube was not processed before it is not processed after - als log as you process by the xmla commands!)

Hannes

Need Information about Processing and Aggregations (Update / Referenced Dimensions / Many to

HI!

I am looking for some information about processing of complex big models with partitioning.

facts:

we need a lot of aggreations

have several dimension with 2 mio. members

have partitioned the cube and want to process only a few and not all partitions

want to process dimension by update (does not unporcess old partitions) or increment update

have some dimensions with references relationship to the partitions

may have linked measuregroups

questions:

update dimensions processing

does this unprocess a cube or partition?

we have the problem that our partitions get unprocessed?

maybe if its referenced by ohter dimension

or it is an parent child dimensions

are there any circumstances where an update dimension unprocess the cube?

how well are aggregations if we process update dimension

how well are aggregations if we process update referenced dimension

howto aggregate with a many to many measuregroup and update dimension

Links to any good documentation would be really interesting.

THANKS

HANNES

Some good material about AS2005 processing model: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sql2k5_asprocarch.asp

First. In AS2005 you have 2 types of processing that update dimension.

One is ProcessUpdate and another one is ProcessAdd.

update dimensions processing|||

Hi Hannes,

Did you find what was the problem that your partitions become unprocessed?

I have the same problem, i'm doing processing to new partitions and the old ones become 'Unprocessed'

Thanks in advance.

|||

It was because this was a test environment and we changed metadata. Added some attributes. This always need to be a full process.

LG, HANNES

|||

thanks for your answer.

I'm doing Process Full to the partitions, and I have the problem above.

the source database was changed but my query that reads the data hasn't changed, means that i have new columns in the fact table and i haven't add it to my query, does it matter? , i didn't changed my measure group at all.

Do you have any suggestion?

Do I need to do ProcessFull to dimensions as well?

thanks.

|||

If you do a full process of a partition the processing state of a cube does not change

(if the cube was prcessed before if is processed after

if the cube was not processed before it is not processed after - als log as you process by the xmla commands!)

Hannes

Monday, February 20, 2012

Need help with update/select statements please.

Hi all,
I have the following data and need to clean up all the junk data and keep
the valid Lastname and FirstName. Please
see the result want below. Thank you very much in advance.
IF OBJECT_ID('Tempdb.dbo.#Temp', 'u') IS NOT NULL
DROP TABLE #Temp
GO
CREATE TABLE #Temp
(
LastName VARCHAR(25) NULL,
FirstName VARCHAR(25) NULL
)
GO
INSERT #Temp VALUES ('##Jairdullo', '##Mary')
INSERT #Temp VALUES ('(6957)BARAHONA', '(6957)MARIA')
INSERT #Temp VALUES ('(ds)Ah', '(ds)Sati')
INSERT #Temp VALUES ('(ds)BURNS', '(ds)CHRISTA')
INSERT #Temp VALUES ('*ALCARAZ', '*MICHAEL')
INSERT #Temp VALUES ('*CERRATO 95066454', '*GLENDA')
INSERT #Temp VALUES ('.', '23CURT')
INSERT #Temp VALUES ('-GREENE', '34DARLENE')
INSERT #Temp VALUES ('/', '?John')
INSERT #Temp VALUES ('~~~FREEMAN', '@.@.@.ANDREW')
INSERT #Temp VALUES ('123Zargarian', '97Lisa')
INSERT #Temp VALUES ('12DE LA CRUZ', 'SANDRA')
INSERT #Temp VALUES ('6957/ Wolfe', '6957/ Donald')
INSERT #Temp VALUES ('!ABBY', 'ABBY')
INSERT #Temp VALUES ('@.ABBOUD FAOUR', 'PARIS')
INSERT #Temp VALUES ('#ABBOTT', '$MONICA')
INSERT #Temp VALUES ('%ABBENHUYS', '^Abbath')
INSERT #Temp VALUES ('&AAMODT', '(MARILYN')
INSERT #Temp VALUES (')%aaland', '-8052160336')
INSERT #Temp VALUES ('a', 'Ksd')
INSERT #Temp VALUES ('ABD-EL-SHAID', 'DELIA')
INSERT #Temp VALUES ('12HARRISON-PEREZ', '@.#3CHRISTINA')
go
SELECT *
FROM #Temp
go
LastName FirstName
-- --
##Jairdullo ##Mary
(6957)BARAHONA (6957)MARIA
(ds)Ah (ds)Sati
(ds)BURNS (ds)CHRISTA
*ALCARAZ *MICHAEL
*CERRATO 95066454 *GLENDA
. 23CURT
-GREENE 34DARLENE
/ ?John
~~~FREEMAN @.@.@.ANDREW
123Zargarian 97Lisa
12DE LA CRUZ SANDRA
6957/ Wolfe 6957/ Donald
!ABBY ABBY
@.ABBOUD FAOUR PARIS
#ABBOTT $MONICA
%ABBENHUYS ^Abbath
&AAMODT (MARILYN
)%aaland -8052160336
a Ksd
ABD-EL-SHAID DELIA
12HARRISON-PEREZ @.#3CHRISTINA
Result want:
--Note: Remove hypen between the Lastname ABD-EL-SHAID with ABDELSHAID
LastName FirstName
-- --
Jairdullo Mary
BARAHONA MARIA
Ah Sati
BURNS CHRISTA
ALCARAZ MICHAEL
CERRATO GLENDA
GREENE DARLENE
FREEMAN ANDREW
Zargarian Lisa
LACRUZ SANDRA
Wolfe Donald
ABBY ABBY
ABBOUDFAOUR PARIS
ABBOTT MONICA
ABDELSHAID DELIA
12HARRISONPEREZ CHRISTINAUPDATE Foobar
SET last_name
= REPLACE( REPLACE( ..
REPLACE(last_name, '-', '')(
'#','')
..);
first_name
= REPLACE( REPLACE( ..
REPLACE(last_name, '-', '')(
'#','')
..);
You can nest the REPLACE() funciton 32 levels deep. This avoids cursors
and other non-relational code.

Need help with UPDATE with PARTITION & PARAMETER

Hi,

I wrote this stored procedure that works, and returns what I want, but now I want to mark the "Active" field to 1 for each of the records returned by this. I have had no luck so far.

ALTER PROCEDURE [dbo].[SelectCurrent_acmdtn]

@.extractNum char(10)

AS

BEGIN

SET NOCOUNT ON;

SELECT id, efctv_from_dt, efctv_to_dt, modify_ts, extractno, Active, acmtdn_RECID

FROM (SELECT dbo.acmdtn.*, row_number() OVER (partition BY id

ORDER BY extractno, efctv_to_dt DESC, efctv_from_dt DESC, modify_ts DESC, acmdtn_RECID DESC) rn

FROM dbo.acmdtn

WHERE extractno > @.extractNum) Rank

WHERE rn = 1

END

I have tried inserting Update between the 2 "WHERE" statements, but it returns an error

"Invalid column name 'rn'."

I have also tried opening the recordset in Access VB, but I am restricted to read-only.

I would prefer to have a stored procedure do this.

I can get it to work if I take out the parameter, but I need that part.

The purpose of this (if you care..) is I have a large amount of historical data (this is one of 42 tables) that I need to run reports on, but I need to have the data "as of a certain date (or extractno)". This is data exported from another application that I only get flat files for, that I have imported into SQL Server tables. So, by running this procedure, I get the latest "id" record as of the extractno (I get a new extract every day, with changes that were made the previous day). I want to mark these latest fields in the "Active" field so when I create reports, I can have them filter on this field.

Any help would be greatly appreciated.

Hi,

I wrote this stored procedure that works, and returns what I want, but now I want to mark the "Active" field to 1 for each of the records returned by this. I have had no luck so far.

ALTER PROCEDURE [dbo].[SelectCurrent_acmdtn]

@.extractNum char(10)

AS

BEGIN

SET NOCOUNT ON;

SELECT id, efctv_from_dt, efctv_to_dt, modify_ts, extractno, Active, acmtdn_RECID

FROM (SELECT dbo.acmdtn.*, row_number() OVER (partition BY id

ORDER BY extractno, efctv_to_dt DESC, efctv_from_dt DESC, modify_ts DESC, acmdtn_RECID DESC) rn

FROM dbo.acmdtn

WHERE extractno > @.extractNum) Rank

WHERE rn = 1

END

I have tried inserting Update between the 2 "WHERE" statements, but it returns an error

"Invalid column name 'rn'."

I have also tried opening the recordset in Access VB , but I am restricted to read-only.

I would prefer to have a stored procedure do this.

I can get it to work if I take out the parameter, but I need that part.

The purpose of this (if you care..) is I have a large amount of historical data (this is one of 42 tables) that I need to run reports on, but I need to have the data "as of a certain date (or extractno)". This is data exported from another application that I only get flat files for, that I have imported into SQL Server tables. So, by running this procedure, I get the each latest "id" record as of the extractno (I get a new extract every day, with changes that were made the previous day). I want to mark these latest fields in the "Active" field so when I create reports, I can have them filter on this field.

Any help would be greatly appreciated.|||

Hi,

I wrote this stored procedure that works, and returns what I want, but now I want to mark the "Active" field to 1 for each of the records returned by this. I have had no luck so far. I am working in SQL server 2005.

ALTER PROCEDURE [dbo].[SelectCurrent_acmdtn]

@.extractNum char(10)

AS

BEGIN

SET NOCOUNT ON;

SELECT id, efctv_from_dt, efctv_to_dt, modify_ts, extractno, Active, acmtdn_RECID

FROM (SELECT dbo.acmdtn.*, row_number() OVER (partition BY id

ORDER BY extractno, efctv_to_dt DESC, efctv_from_dt DESC, modify_ts DESC, acmdtn_RECID DESC) rn

FROM dbo.acmdtn

WHERE extractno > @.extractNum) Rank

WHERE rn = 1

END

I have tried inserting Update between the 2 "WHERE" statements, but it returns an error

"Invalid column name 'rn'."

I have also tried opening the recordset in Access VB , but I am restricted to read-only.

I would prefer to have a stored procedure do this.

I can get it to work if I take out the parameter, but I need that part.

The purpose of this (if you care..) is I have a large amount of historical data (this is one of 42 tables) that I need to run reports on, but I need to have the data "as of a certain date (or extractno)". This is data exported from another application that I only get flat files for, that I have imported into SQL Server tables. So, by running this procedure, I get the latest "id" record as of the extractno (I get a new extract every day, with changes that were made the previous day). I want to mark these latest fields in the "Active" field so when I create reports, I can have them filter on this field.

Any help would be greatly appreciated.|||

You are returning the results of a data manipulation - there may not be a 'match' between the resultset and the actual table.

My 'simple' recommendation is to:

capture the resultset in a @.Table variable, then use the [ID] value from that table variable to UPDATE the data, finally, returning the contents of the table variable with a SELECT.|||

Thanks for the reply. I posted this question a few too many times (was told it was deleted by administrator!).

Anyway, [ID] is not the primary key, [acmdtn_RECID] is. Any changes made to the master database simply add another record, and retain the old record, so historical queries can be run. So there can be dozens of records for each [ID]. What I want is the most recent record, for each [ID], as of a certain date.

I guess I just don't get why I can display the information, but I can't write back to the database based on that displayed information. It's a complex query, but there's no joins, or other tables involved, there can only be a 1:1 relationship between records in the table, and records in the resultset.

Need help with Update statement; DDL included

Four table: Securities, Positions, ExchangeList and ExchangeListMember
Securities has an SecurityID field and the exchange it is traded on.
Positions has a SecurityID and an Account.
ExchangeList is used to define the exchanges that an account is permitted to
own securities on. The ExchangeList.ExchangeListCode would contain an
account code.
ExchangeListMember contains the specific exchanges permitted for an account.
For example Account A can own securities on the NYS and BUE exchanges.
I have created a temp table called #TempPositions which contains all
positions. I want to update #TempPositions by placing an asterisk before the
SecurityID in records where the security is NOT permitted because the
securitys' exchange is NOT permissible.
UPDATE #TempPositions SET #TempPositions.SecurityID = '*' + #TempPositions
.SecurityID
WHERE .....
Here are my expected results for SELECT * FROM #TempPositions after the
update.
1,A
*2,A
3,A
3,B
4,B
*5,B
6,C
7,C
*1,D
*2,D
More narrative:
Account A is permitted to own securities on the NYS and BUE exchanges. It
has a position with SecurityID =2 which is traded on CBT. This position is
restricted.
Account B is permitted to own securities on the BUE and CPH exchanges. It
has a position with SecurityID =5 which is traded on NAS. This position is
restricted.
Account C does not have an ExchangeList. No restrictions.
Account D is permitted to own securities on the NAS exchange only. It has a
position with SecurityID =1 which is traded on NYS. It has a position with
SecurityID =2 which is traded on CBT .These positions is restricted.
This would be easier for me if the ExchangeList table defined restricted
exchanges rather than permitted exchanges. Any help would be appreciated.
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[Securities]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Securities]
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[Positions]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Positions]
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[ExchangeList]') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
drop table [dbo].[ExchangeList]
GO
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[ExchangeListMember]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
drop table [dbo].[ExchangeListMember]
GO
CREATE TABLE [dbo].[Securities] (
[SecurityID] [int] NOT NULL ,
[Exchange] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Positions] (
[SecurityID] [int] NOT NULL ,
[Account] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[ExchangeList] (
[ExchangeListCode] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
,
[ExchangeListName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[ExchangeListMember] (
[ExchangeListCode] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
,
[Exchange] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[#TempPositions] (
[SecurityID] [int] NOT NULL ,
[Account] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Securities] ADD
CONSTRAINT [PK_Securities] PRIMARY KEY CLUSTERED
(
[SecurityID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Positions] ADD
CONSTRAINT [PK_Positions] PRIMARY KEY CLUSTERED
(
[SecurityID],
[Account]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ExchangeList] ADD
CONSTRAINT [PK_ExchangeList] PRIMARY KEY CLUSTERED
(
[ExchangeListCode]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ExchangeListMember] ADD
CONSTRAINT [PK_ExchangeListMember] PRIMARY KEY CLUSTERED
(
[ExchangeListCode],
[Exchange]
) ON [PRIMARY]
GO
INSERT Securities (SecurityID,Exchange) VALUES (1,'NYS')
INSERT Securities (SecurityID,Exchange) VALUES (2,'CBT')
INSERT Securities (SecurityID,Exchange) VALUES (3,'BUE')
INSERT Securities (SecurityID,Exchange) VALUES (4,'CPH')
INSERT Securities (SecurityID,Exchange) VALUES (5,'NAS')
INSERT Securities (SecurityID,Exchange) VALUES (6,'IRL')
INSERT Securities (SecurityID,Exchange) VALUES (7,'JPN')
INSERT Securities (SecurityID,Exchange) VALUES (8,'KOR')
GO
INSERT Positions (SecurityID,Account)VALUES (1,'A')
INSERT Positions (SecurityID,Account)VALUES (2,'A')
INSERT Positions (SecurityID,Account)VALUES (3,'A')
INSERT Positions (SecurityID,Account)VALUES (3,'B')
INSERT Positions (SecurityID,Account)VALUES (4,'B')
INSERT Positions (SecurityID,Account)VALUES (5,'B')
INSERT Positions (SecurityID,Account)VALUES (6,'C')
INSERT Positions (SecurityID,Account)VALUES (7,'C')
INSERT Positions (SecurityID,Account)VALUES (1,'D')
INSERT Positions (SecurityID,Account)VALUES (2,'D')
GO
INSERT ExchangeList (ExchangeListCode,ExchangeListName) VALUES ('A', 'A
Permissible Exchanges')
INSERT ExchangeList (ExchangeListCode,ExchangeListName) VALUES ('B', 'B
Permissible Exchanges')
INSERT ExchangeList (ExchangeListCode,ExchangeListName) VALUES ('D', 'D
Permissible Exchanges')
INSERT ExchangeListMember (ExchangeListCode,Exchange) VALUES ('A','NYS')
INSERT ExchangeListMember (ExchangeListCode,Exchange) VALUES ('A','BUE')
INSERT ExchangeListMember (ExchangeListCode,Exchange) VALUES ('B','BUE')
INSERT ExchangeListMember (ExchangeListCode,Exchange) VALUES ('B','CPH')
INSERT ExchangeListMember (ExchangeListCode,Exchange) VALUES ('B','NAS')
INSERT ExchangeListMember (ExchangeListCode,Exchange) VALUES ('D','NAS')
INSERT #TempPositions (SecurityID,Account)(SELECT * FROM Positions)Terri wrote:
Thanks for the DDL.
I tried it out but I'm .
Your narrative explanation doesn't seem to completely fit the data you
supplied or I'm looking at it the wrong way.
However I think this is what you're looking for:
alter table #TempPositions add Restricted bit default 0 NOT NULL
UPDATE #TempPositions SET #TempPositions.Restricted = 1
WHERE securityID not in (select securities.SecurityID from
ExchangeListMember inner join securities on securities.SecurityID =
#TempPositions.SecurityID and ExchangeListCode = #TempPositions.Account
and securities.Exchange = ExchangeListMember.Exchange)
You cannot add a asterix to a integer column so I added a column named
Restricted and flag it 1 if it is.
HTH,
Stijn Verrept.|||Thanks Stijn, Sorry about me trying to add an asterisk to an integer field.
Altering the table is fine.
I had tried something like your solution but the problem is with Account C.
Account C doesn't have an ExchangeList so securities should never be
restricted. The subselect somehow needs to take into account "accounts" with
no ExchangeList and make sure the securities of those accounts are not
flagged as restricted.
"Stijn Verrept" <stjin@.entrysoft.com> wrote in message
news:1sqdnTV81ZHwoDreRVny0A@.scarlet.biz...
> Terri wrote:
> Thanks for the DDL.
> I tried it out but I'm .
> Your narrative explanation doesn't seem to completely fit the data you
> supplied or I'm looking at it the wrong way.
> However I think this is what you're looking for:
> alter table #TempPositions add Restricted bit default 0 NOT NULL
> UPDATE #TempPositions SET #TempPositions.Restricted = 1
> WHERE securityID not in (select securities.SecurityID from
> ExchangeListMember inner join securities on securities.SecurityID =
> #TempPositions.SecurityID and ExchangeListCode = #TempPositions.Account
> and securities.Exchange = ExchangeListMember.Exchange)
>
> You cannot add a asterix to a integer column so I added a column named
> Restricted and flag it 1 if it is.
> --
> HTH,
> Stijn Verrept.|||Terri wrote:

> Thanks Stijn, Sorry about me trying to add an asterisk to an integer
> field. Altering the table is fine.
> I had tried something like your solution but the problem is with
> Account C. Account C doesn't have an ExchangeList so securities
> should never be restricted. The subselect somehow needs to take into
> account "accounts" with no ExchangeList and make sure the securities
> of those accounts are not flagged as restricted.
Aha! That's probably what I didn't understand, then this will be
better :)
UPDATE #TempPositions SET #TempPositions.Restricted = 1
WHERE securityID not in (select securities.SecurityID from
ExchangeListMember inner join securities on securities.SecurityID =
#TempPositions.SecurityID and ExchangeListCode = #TempPositions.Account
and securities.Exchange = ExchangeListMember.Exchange)
and (select count(*) from ExchangeListMember where ExchangeListCode =
#TempPositions.Account) > 0
HTH,
Stijn Verrept.|||"Stijn Verrept" <stjin@.entrysoft.com> wrote in message
news:T9qdnWst9ZU81TrenZ2dnUVZ8qidnZ2d@.sc
arlet.biz...
> Aha! That's probably what I didn't understand, then this will be
> better :)
Perfect, thanks so much.|||>> I have created a temp table called #TempPositions which contains all posi
tions. I want to update #TempPositions by placing an asterisk [in violation
of the rule about not formatting display data in the database!!] before the
SecurityID in records [sic
] where the security is NOT permitted because the securitys' exchange is NOT
permissible. <<
Have you thought about using a VIEW that would always be up to date,
instead of constantly updating a temp table in proprietary syntax?
But the real point is that this is a constraint and needs to be done
with REFERENCES clauses and a proper design -- NOT a temp table at all!
Also, ask yourself why you have two tables with the SAME structure in
violation of the rules of any data modeling?|||"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1135050199.293240.265890@.g14g2000cwa.googlegroups.com...
positions. I want to update #TempPositions by placing an asterisk [in
violation of the rule about not formatting display data in the database!!]
before the SecurityID in records [sic] where the security is NOT permitted
because the securitys' exchange is NOT permissible. <<
> Have you thought about using a VIEW that would always be up to date,
> instead of constantly updating a temp table in proprietary syntax?
> But the real point is that this is a constraint and needs to be done
> with REFERENCES clauses and a proper design -- NOT a temp table at all!
> Also, ask yourself why you have two tables with the SAME structure in
> violation of the rules of any data modeling?
>
My employer strongly discourages me from presenting real data or table
structure so I may post a problem that helps me solve my issue but doesn't
reveal any information about my data, data structure, or systems. I suspect
others do this is well so I don't think it's safe to assume that posted data
structure or methods are used in production. They could merely serve as a
demonstration of a related problem.
In this case I am actually using temp tables in a stored procedure. I update
my temp table 6 times in the procedure. Perhaps someone could write a single
select statement which would return the results I need, but not me at this
point. I consider the logic of the procedure to be highly complex. I find it
easier to achieve my desired result with a series of updates against temp
tables although I realize this is not ideal from an academic point of view.
The procedure takes <4 seconds to run and is only run in production several
times a w so I consider the performance acceptable. Others who have
viewed this procedure find it easier to comprehend. I find it easier to
maintain and modify. I understand your point and will consider it in the
future.

Need help with Update query to populate new field

Hi folks:

I have added a new field to an established table, and am having trouble figuring out how to populate its values:

Two tables are involved: Jobs and Parts

There is a one-to-one relationship between each JobID and its PartID

Each Part has a PartPrice. Now I have added to the Jobs table a JobPrice field. Whenever a new Job is created, JobPrice takes the current value of its Part's PartPrice. Each Job's JobPrice remains constant for historical purposes, while the PartPrice may fluctuate at my client's whim.

The trouble is that the Jobs table is 10k+ records large, and I need to fill the JobPrice values. I am at a loss. I know how to commit the update one record at a time:

UPDATE Jobs

SET JobPrice = (SELECT PartPrice FROM Parts WHERE (PartID = [the part in question]))

WHERE (JobID = [the job in question])

My SQL knowledge is limited to basic statements that I use in my .NET work, and I rarely create anything in Management Studio more elaborate than what you see above. Many thanks for your time,

Matt

which is the parent table?

|||assuming that the jobs table has a foreign key that links to the parts table, the update query should look like this

UPDATE J
set j.JobPrice = p.PartPrice
from Jobs J
join Parts p on (p.PartID = j.PartID)|||

Thanks for your replies. I don't have any knowledge about foreign keys, etc. I attempted to set one up between the Jobs and Parts tables, but SQL Server Mgt Studio is still throwing errors when I attempt your query, carlop.

Thank you both anyway for throwing a pearl or two before this swine.

m