Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Thursday, March 29, 2012

Combining two tables to make a third

Need help combining two tables into a third with corresponding fields
of both tables
table 1
time 12 mike work
time 13 john sleep
times 24 George jump
table 2
23 sam run
There is a table3 which has all the columns of the two tables but i
cannot seem to come around to combine them
time 12 mike work 23 sam run
time 13 john sleep 23 sam run
times 24 George jump 23 sam run
Your help is appreciated have been on thisI guess a cross join will work since I don't see any keys
select * into table3 from table1 cross joins table2
select * from table3
Denis the SQL Menace
http://sqlservercode.blogspot.com/
mngong@.gmail.com wrote:
> Need help combining two tables into a third with corresponding fields
> of both tables
> table 1
> time 12 mike work
> time 13 john sleep
> times 24 George jump
> table 2
> 23 sam run
> There is a table3 which has all the columns of the two tables but i
> cannot seem to come around to combine them
> time 12 mike work 23 sam run
> time 13 john sleep 23 sam run
> times 24 George jump 23 sam run
> Your help is appreciated have been on this|||I really don't know what you are asking for, but I suppose I can
guess.
SELECT Table1.*, Table2.*
FROM Table1 CROSS JOIN Table2
Roy Harvey
Beacon Falls, CT
On 27 Jul 2006 06:24:48 -0700, mngong@.gmail.com wrote:
>Need help combining two tables into a third with corresponding fields
>of both tables
>table 1
>time 12 mike work
>time 13 john sleep
>times 24 George jump
>table 2
>23 sam run
>There is a table3 which has all the columns of the two tables but i
>cannot seem to come around to combine them
>time 12 mike work 23 sam run
>time 13 john sleep 23 sam run
>times 24 George jump 23 sam run
>Your help is appreciated have been on this|||This is a multi-part message in MIME format.
--=_NextPart_000_0C6D_01C6B148.4585B4F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Based on this limited information:
INSERT INTO Table3
SELECT *
FROM Table1
CROSS JOIN Table2
If you must control the field order, you may need to list the columns =from each table instead of using [*],
-- Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience. Most experience comes from bad judgment. - Anonymous
<mngong@.gmail.com> wrote in message =news:1154006687.920219.20920@.75g2000cwc.googlegroups.com...
> Need help combining two tables into a third with corresponding fields
> of both tables
> table 1
> time 12 mike work
> time 13 john sleep
> times 24 George jump
> table 2
> 23 sam run
> There is a table3 which has all the columns of the two tables but i
> cannot seem to come around to combine them
> time 12 mike work 23 sam run
> time 13 john sleep 23 sam run
> times 24 George jump 23 sam run
> > Your help is appreciated have been on this
>
--=_NextPart_000_0C6D_01C6B148.4585B4F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Based on this limited =information:
INSERT INTO Table3
SELECT =*
FROM =Table1
=CROSS JOIN Table2
If you must control the field order, =you may need to list the columns from each table instead of using [*],
-- Arnie Rowland, =Ph.D.Westwood Consulting, Inc
Most good judgment comes from =experience. Most experience comes from bad judgment. - Anonymous
wrote in message news:1154006687.920219.20920@.75g2000cwc.googlegroups.com...> =Need help combining two tables into a third with corresponding fields> of =both tables> table 1> time 12 mike work> =time 13 john sleep> times 24 George jump> table 2> 23 sam run> There is a table3 which has all the columns of the two =tables but i> cannot seem to come around to combine them> time =12 mike work 23 sam run> time 13 john sleep 23 =sam run> times 24 George jump 23 sam run> > Your help =is appreciated have been on this>

--=_NextPart_000_0C6D_01C6B148.4585B4F0--

Combining Time with a Date

Let's say that I have two tables, one of which has a StartDate field (say,
TableA) as well as a foreign key reference to TableB which has a field
called BeginTime. Suppose that values in TableA.StartDate field have varying
dates together with a time of 00:00:00.000 (as it's a datetime field) and
TableB.BeginTime fields have varying time values (e.g. 07:00:00.000). NOTE:
TableB.BeginTime field is currently an nvarchar field.
What I want to do is compare the current date (using GETDATE()) to the
datetime value that results from combining TableA.StartDate with
TableB.BeginTime. For example, if TableA.StartDate = "03/29/2005
00:00:00.000", and TableB.BeginTime = "07:00:00.000", I want to compare the
current date to "03/29/2005 07:00:00.000". I will ultimately be trying to
determine if the difference between them is greater than a certain # of
minutes. How could I do this using SQL?Bob
Look at DATEDIFF system function.
"BobRoyAce" <bob@.decisioncritical.com> wrote in message
news:%23byP4xCNFHA.3844@.TK2MSFTNGP14.phx.gbl...
> Let's say that I have two tables, one of which has a StartDate field (say,
> TableA) as well as a foreign key reference to TableB which has a field
> called BeginTime. Suppose that values in TableA.StartDate field have
varying
> dates together with a time of 00:00:00.000 (as it's a datetime field) and
> TableB.BeginTime fields have varying time values (e.g. 07:00:00.000).
NOTE:
> TableB.BeginTime field is currently an nvarchar field.
> What I want to do is compare the current date (using GETDATE()) to the
> datetime value that results from combining TableA.StartDate with
> TableB.BeginTime. For example, if TableA.StartDate = "03/29/2005
> 00:00:00.000", and TableB.BeginTime = "07:00:00.000", I want to compare
the
> current date to "03/29/2005 07:00:00.000". I will ultimately be trying to
> determine if the difference between them is greater than a certain # of
> minutes. How could I do this using SQL?
>|||I am familiar with the DATEDIFF function, but that will not combine separate
DATE and TIME values together to give me a DATETIME. That's the piece I'm
missing here.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:u1zyYHDNFHA.3076@.tk2msftngp13.phx.gbl...
> Bob
> Look at DATEDIFF system function.
>
> "BobRoyAce" <bob@.decisioncritical.com> wrote in message
> news:%23byP4xCNFHA.3844@.TK2MSFTNGP14.phx.gbl...
> varying
> NOTE:
> the
>|||Since you express datetime as a string, it is just a matter of building a st
ring expression which
can safely be converted to datetime. I didn't follow your first post, but le
ts assume that one value
is datetime and the other is a string:
DECLARE @.a datetime, @.b nvarchar(40)
SET @.a = getdate()
SET @.b = '07:00:00'
SELECT CAST(CONVERT(char(8), @.a, 112) + ' ' + @.b AS datetime)
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"BobRoyAce" <bob@.decisioncritical.com> wrote in message
news:%23mQ5VNDNFHA.244@.TK2MSFTNGP12.phx.gbl...
>I am familiar with the DATEDIFF function, but that will not combine separat
e DATE and TIME values
>together to give me a DATETIME. That's the piece I'm missing here.
> "Uri Dimant" <urid@.iscar.co.il> wrote in message news:u1zyYHDNFHA.3076@.tk2
msftngp13.phx.gbl...
>|||What's the 112 for?|||>> What's the 112 for?
It is the argument for CONVERT to return the ISO format( yymmdd ) for dates
represented as a string. See the topic CAST and CONVERT in SQL Server Books
Online.
Anithsqlsql

Sunday, March 25, 2012

Combining info from two tables?

Hello, I'm having some problems trying to access two tables in a SQL database at the same time and making some results out of them. Let me explain further: the first table has some information in that I'm going to be doing a select query on and reading out, but one of the columns in this table is a set of codes, the second table contains the codes in one column and their meanings in the other.

So I want to bring back the information from the first table and then select the information for the codes shown from the second table and print their meanings alongside the information from the first table. Could anyone help me out in figuring out how my SQL in the ASP page for this would be written? Sorry if this is a little confusing but im having a hard time visualising how to do this.Your query will look something like this:

select t1.col1, t1.col2, t1.col3, t1.code, t2.description
from t1
join t2 on t1.code = t2.code
where ...;|||Cheers, it works. I've only been using very basic SQl views in the past so joins etc are new to me. I do have another question tho, I'm writing these queries in an ASP page and they are very long single lines at the mo, how do I break them up into seperate shorter lines?|||Something like this, if I recall correctly:

strSQL = "select empno" _
& " from emp" _
& " where ename = ?"

i.e. the underscore is the "continued on next line" marker for VBScript.|||Thanks again for you help andrew|||Ok i've hit another snag with this, there are two different columns in the first table that are codes that need to be compared to the code listing in the 2nd table and then their code meanings sent back. I ve done this with one which is where u're first piece of code comes in handy but i can't join two columns in the 1st table to the one in the 2nd.|||Oh no, not the "One True Look-up Table"? :(

You can join to the same table twice like this:
select t1.col1, t1.col2, t1.col3, t1.code1,
t2_1.description, t1.code2, t2_2.description
from t1
join t2 t2_1 on t1.code1 = t2_1.code
join t2 t2_2 on t1.code2 = t2_2.code
where ...;
t2_1 and t2_2 are "aliases" for the table t2 which now appears twice in the query.|||that's funny, i took "one of the columns in this table is a set of codes" to mean something completely different (a denormalized table)

looks like you may have understood the requirements better than i did, tony

see http://forums.devshed.com/t193376/s.html|||Who knows? Maybe you are right - it's equally possible!|||Sorry guys I think i'm over complicating the matter, let me try to explain it in a minimal way,

Table 1 with 3 columns, we'll call it date, the next column we will call code1 and the last column we will call code2.

Table 2 has 2 columns, one called error_codes and another called translation with is the text meaning of the error codes.

I want the query to select all the records in the first table for a certain criteria and give the translations for the code columns in the first table from the 2nd table.|||Well, I think I have already given you the SQL for that above.|||I've actually found that the 2nd column in the first table referred to a different set of codes in another table so the join will work now, thank you both for your help, i'm learning it bit by bit!sqlsql

Combining date field and time field in a column

SELECT RequireDate + ' ' + RequireTime AS dat
FROM IN_Heade
My Database
Date Tim
28/03/2004 01:34:09P
After run SQL statement, my result become
26/03/2004 01:34:09P
Why my date minus two day? so what should i need to do? Urgent please reply to me at Babies001@.yahoo.co
ThankWhat datatypes are you using for storing your date and time values? If using
strings, then concatenate them and use CAST or CONVERT functions to convert
the concatenated value into a datetime value.
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"TM" <anonymous@.discussions.microsoft.com> wrote in message
news:E6A4D56F-97F0-4E97-AD2D-3348504128DF@.microsoft.com...
SELECT RequireDate + ' ' + RequireTime AS date
FROM IN_Header
My Database:
Date Time
28/03/2004 01:34:09PM
After run SQL statement, my result become:
26/03/2004 01:34:09PM
Why my date minus two day? so what should i need to do? Urgent please reply
to me at Babies001@.yahoo.com
Thanks|||Regarding the Question, my database fiel
Field DataTyp
Date DateTim
Time DateTim
So my result become
SELECT RequireDate + ' ' + RequireTime AS dat
FROM IN_Heade
My Database
Date Tim
28/03/2004 01:34:09P
After run SQL statement, my result become
26/03/2004 01:34:09P
What the code for convert the date and time together and my date will not minus two day
Can adding the source code inside
Thank
-- Narayana Vyas Kondreddi wrote: --
What datatypes are you using for storing your date and time values? If usin
strings, then concatenate them and use CAST or CONVERT functions to conver
the concatenated value into a datetime value
--
HTH
Vyas, MVP (SQL Server
http://vyaskn.tripod.com
Is .NET important for a database professional
http://vyaskn.tripod.com/poll.ht
"TM" <anonymous@.discussions.microsoft.com> wrote in messag
news:E6A4D56F-97F0-4E97-AD2D-3348504128DF@.microsoft.com..
SELECT RequireDate + ' ' + RequireTime AS dat
FROM IN_Heade
My Database
Date Tim
28/03/2004 01:34:09P
After run SQL statement, my result become
26/03/2004 01:34:09P
Why my date minus two day? so what should i need to do? Urgent please repl
to me at Babies001@.yahoo.co
Thanksqlsql

Combining date and time

I have 2 fields, one containing just the date and another containing the time. I want to combine them into a proper date/time format. No matter what I try, I lose 2 days in the process. Combining

2005-12-21 00:00:00.000
and
1899-12-30 14:30:00.000

will get me the 19th at 14:30. What am I missing here?

TIAThis was addressed recently in the following thread: link (http://www.dbforums.com/t1203973.html)

Regards,

hmscott|||Thank you. I searched and somehow missed that thread. Bad choice of search words I suppose. I guess my problem was not using varchar. I had tried using convert but used datetime as the type, using a style of 101 for the date portion and 108 for the time. This seems to be working correctly:

Convert(datetime,Convert(varchar(11), DateField, 101) + ' ' + Convert(varchar(8), ReqTime, 108))

Thursday, March 22, 2012

Combining a Line Graph and Scatter Graph

I have to make a report of a combination of salary trends of
employees. The idea is to show the salary of a employee for a certain
period of time, also showing where he stands when he compared to
salaries of other employees.
I will show the salary of the employee in a line Graph, thats fine I
have already done it. X axis -> Employment Time, Y-axis Salary The
qquery will be like
userid, date, salary
72, 01/01/2001, 1000
72, 06/01/2001, 1600
72, 01/01/2002, 6000
72, 06/01/2002, 7080
72, 01/01/2003, 8010
72, 06/01/2003, 10000
72, 01/01/2004, 10050
72, 06/01/2004, 15500
salary in the Values section
date in the Category Section
The Line Graph is done.
Assuming I have the same query with more userid values (For other
employees also). I have to plot scatter points around the Line Graph
showing the salaries of each other employee as against the employee
72.
So the Graph will have 1 line for userid 72, scatter points around
that line.
How can I do it. Working Ideas please.
Thanks !
Anand Sagar
Now I have to show scatter pointsanybody there !?

Tuesday, March 20, 2012

combine separate date & time fields into one datetime field?

Good morning.

I am importing an XLS file into one of my tables. The fields are:

Date Id Time IO

12/22/2006

2

12:48:45 PM

9

12/22/2006

16

5:40:55 AM

1

12/22/2006

16

12:03:59 PM

2


When I do the import, I get the following:

Date Id Time IO
12/22/2006 12:00:00AM 2 12/30/1899 12:48:45 PM 2
12/22/2006 12:00:00AM 16 12/30/1899 5:40:55 AM 1
12/22/2006 12:00:00AM 16 12/30/1899 12:03:59 PM 2

Here are my doubts:

1. Is it be better to combine the Date & Time fields into one column? Advantages/Disadvantages?
2. If I don't combine them, should I use varchar or datetime data type?
2. What issues or problems might I have when I program SQL reports, if I leave the fields as they are?

Any comments or suggestions will be very much welcomed.

Cheers mates.I was suggested to try this out:

UPDATE tbl
SET Date = Date + convert(char(8), Time, 108)

I'll run it after I use DTwizard to export the data into my table. I should also mention I have no PKs defined, just a FK that references Id from a table called Employees. I'm thinking it's best to define ID and Date and Time as PKs.|||

As far as SQL Server is concerned, it will be far easier over time to work with and deal with date/time data if it is stored as one column. I recommend combining the two.

One primary reason is that the combined column will allow easier datetime comparisions and searches.

combine separate date & time fields into one datetime field?

Good morning.

I am importing an XLS file into one of my tables. The fields are:

Date Id Time IO
12/22/2006 2 12:48:45 PM 9
12/22/2006 16 5:40:55 AM 1
12/22/2006 16 12:03:59 PM 2

When I do the import, I get the following:

Date Id Time IO
12/22/2006 12:00:00AM 2 12/30/1899 12:48:45 PM 2
12/22/2006 12:00:00AM 16 12/30/1899 5:40:55 AM 1
12/22/2006 12:00:00AM 16 12/30/1899 12:03:59 PM 2

Here are my doubts:

1. Would it be better to combine the Date & Time fields into one
column? If so, how?
2. What issues or problems might I have when I program SQL reports, if
I leave the fields as they are?

Any comments or suggestions will be very much welcomed.

Cheers mates.drurjen (jfontecha@.gmail.com) writes:

Quote:

Originally Posted by

Good morning.
>
I am importing an XLS file into one of my tables. The fields are:
>
Date Id Time IO
12/22/2006 2 12:48:45 PM 9
12/22/2006 16 5:40:55 AM 1
12/22/2006 16 12:03:59 PM 2
>
When I do the import, I get the following:
>
Date Id Time IO
12/22/2006 12:00:00AM 2 12/30/1899 12:48:45 PM 2
12/22/2006 12:00:00AM 16 12/30/1899 5:40:55 AM 1
12/22/2006 12:00:00AM 16 12/30/1899 12:03:59 PM 2
>
Here are my doubts:
>
1. Would it be better to combine the Date & Time fields into one
column? If so, how?


Most probably. (In the end it depends on business needs, which I don't
anything about.)

A way to merge the columns would be:

UPDATE tbl
SET Date = Date + convert(char(8), Time, 108)

Quote:

Originally Posted by

2. What issues or problems might I have when I program SQL reports, if
I leave the fields as they are?


That you get 1899-12-30 printed all over the place, which you probably
don't want to. So you will need a lot of code to filter the date away.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland,

Thank you for replying. Basically the DB is for employee time
attendance records. I start out with a flat txt file and run that
through an Excel macro that:

a) eliminates repeat entries in a time lapse of 5min
b) erases null entries.

I then take the XLS file and use DTWizard to export it into a table
with the same fields as before: Date, Id, Time, IO. I have no primary
keys defined in this table, just a FK (Id). I believe the primary keys
should be ID, Date & Time.

I'll try your suggestion. Thx again, and sorry for the repeat post.

combine OLTP with Star design

Hi,

We are working with AS for some time. We are using OWC as front end. I would like to display in the pivottable attributes of the dimension that are coming from the OLTP. For instance. I have a pivottable sales, customer by time. Next to the customer name I would like to have a column with his phone number and another with his address. Is something like that possible? [I am not concerned about the roll up - the information should only be displayed at leaf level]

To complicate the scenario, I would like to have an additional column called projection. Where the user can edit/add yearly projections for that customer. Any idea how to implement something like that?

Raphael

There are two problems you should recognize in here.

One is how to define your Analysis Services cube to include addtional information about customer phone nuber and address. For this, take a look at AdventureWorks sample project installed as part of SQL Server setup. Take a look at the Customer dimension and see how Phone and other attributes defined there.

Second, is the problem on how to display customer information in your application. BI Dev studio is using OWC as well. You can drag Customer dimension on Rows in cube browser. Drill down to the lowest level (Customer). Now right click on any member on the lowest level and select "Show properties in the report->Select All properties" and you will see all of the Customer's properties.

To be able to modify AS data directly in your application, it should support "writeback" feature. Basically it needs to know how to submit changes back to Analysis Serivices. Take a look if OWC supports writing data back to Analysis Services.

Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.

Monday, March 19, 2012

Combination of Filegroup and Point in Time restore possible?

SQL2K5
Ive been doing some testing this morning and it appears that it is not
possible to combine PIT restores with Filegroup restores, is that correct? I
restore a Filegroup backup, and then the Trans Log backup, but there is no
option to stop at a specific time when doing this. It would sort of make
sense I suppose. It could be argued that doing so would leave the DB in an
inconsistent state if a table that resided outside of the restored Filegroup
had been updated before the restores began, but I just wanted to confirm my
findings(?).
I think the down side to this protection would be that if you are relying on
Filegroup backups/ restores, how would you recover a data loss that had
occured due to human error (someone deletes something they shouldn't have)?
After all, to bring the tables in the restored filegroup online I MUST take a
Trans Log backup and restore it, therefore my accidentally deleted data is
still not recovered.
Does this all sound right, or am I missing something here?
All insights are appreciated.Hi Chris
This sounds like your understanding is correct.
When doing a filegroup restore, SQL Server is assuming that other filegroups
are available, with transactions that might have occurred after the backup
of the filegroup that you are restoring. Restoring a FG requires that you
apply transaction logs to bring the FG up to the same point as the rest of
the database.
The point of FG restore is to recover from hardware errors on a single file
or fg. If you need to recover from user error, you'll need to restore the
whole db.
--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://sqlblog.com
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:9D74B6E1-CE97-4B5F-9E97-C0F946ECA811@.microsoft.com...
> SQL2K5
> Ive been doing some testing this morning and it appears that it is not
> possible to combine PIT restores with Filegroup restores, is that correct?
> I
> restore a Filegroup backup, and then the Trans Log backup, but there is no
> option to stop at a specific time when doing this. It would sort of make
> sense I suppose. It could be argued that doing so would leave the DB in an
> inconsistent state if a table that resided outside of the restored
> Filegroup
> had been updated before the restores began, but I just wanted to confirm
> my
> findings(?).
> I think the down side to this protection would be that if you are relying
> on
> Filegroup backups/ restores, how would you recover a data loss that had
> occured due to human error (someone deletes something they shouldn't
> have)?
> After all, to bring the tables in the restored filegroup online I MUST
> take a
> Trans Log backup and restore it, therefore my accidentally deleted data is
> still not recovered.
> Does this all sound right, or am I missing something here?
> All insights are appreciated.|||Thanks!
"Kalen Delaney" wrote:
> Hi Chris
> This sounds like your understanding is correct.
> When doing a filegroup restore, SQL Server is assuming that other filegroups
> are available, with transactions that might have occurred after the backup
> of the filegroup that you are restoring. Restoring a FG requires that you
> apply transaction logs to bring the FG up to the same point as the rest of
> the database.
> The point of FG restore is to recover from hardware errors on a single file
> or fg. If you need to recover from user error, you'll need to restore the
> whole db.
> --
> HTH
> Kalen Delaney, SQL Server MVP
> www.InsideSQLServer.com
> http://sqlblog.com
>
> "ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
> news:9D74B6E1-CE97-4B5F-9E97-C0F946ECA811@.microsoft.com...
> > SQL2K5
> >
> > Ive been doing some testing this morning and it appears that it is not
> > possible to combine PIT restores with Filegroup restores, is that correct?
> > I
> > restore a Filegroup backup, and then the Trans Log backup, but there is no
> > option to stop at a specific time when doing this. It would sort of make
> > sense I suppose. It could be argued that doing so would leave the DB in an
> > inconsistent state if a table that resided outside of the restored
> > Filegroup
> > had been updated before the restores began, but I just wanted to confirm
> > my
> > findings(?).
> >
> > I think the down side to this protection would be that if you are relying
> > on
> > Filegroup backups/ restores, how would you recover a data loss that had
> > occured due to human error (someone deletes something they shouldn't
> > have)?
> > After all, to bring the tables in the restored filegroup online I MUST
> > take a
> > Trans Log backup and restore it, therefore my accidentally deleted data is
> > still not recovered.
> >
> > Does this all sound right, or am I missing something here?
> >
> > All insights are appreciated.
>
>

Sunday, March 11, 2012

Columns_Updated () and Update() functions

Hello all,

I'm trying to apply an audit DB trigger on a master-detail tables ,I do not need to insert record in the audit master every time an update happend in the audit detail,I tried to use columns_updated() > 0 but it didn't work the way I axpected it to be ..it stopped inserting into the audit master even if an update was applied against the master table ...any help please and I use the Update() function ? is their any major difference?

your help is appreciated

Thanks

Alaa M

COLUMNS_UPDATED() returns a value as a VARBINARY datatype, so for your comparison to work you need to convert the value to an INT first, like this:

CAST(COLUMNS_UPDATED() AS INT)

Chris

|||

Thanks CHRIS

Actually it didn't work ..

or may be i'm missing something here ,anyway this is the code I'm using if you or anyone can point the wrong thing that I'm doing here will be great.

this is the trigger for the master table ,and I only need to insert the changes applied against this table into Audit master no matter how many updates applied to the detail table.

ALTER Trigger trig_Audit_Upd
on [dbo].[Master]

For Update
--WITH ENCRYPTION
AS
IF EXISTS
(
SELECT 'True'
FROM deleted d
LEFT JOIN [Master] A
ON d.MasterID = A.MasterID
)

if (CAST(COLUMNS_UPDATED() AS INT ) & 255) > 0

begin
INSERT INTO [dbo].[syslogAuditMaster](
[MasterID], [Name], [CodeIDType], [DateStarted], [DateDue], [CodeIDReminderType],
[CodeIDAuditStatus], [NoteID], [DeletedOnDate], [Event])
Select del.[MasterID],del.[Name],del.[CodeIDType],del.[Datestarted],del.[Datedue],del.[CodeIDReminderType],
del.[CodeIDAuditStatus], del.[NoteID], del.[DeletedOnDate], 'UPDATE'
FROM deleted del
WHERE del.MasterID = MastertId

end;

thank

|||

I get the feeling that you might be trying to over-complicate things. I can't see the point of explicitly checking for updated columns using the COLUMNS_UPDATED() function - the fact that at least one row has been updated is implied by the existence of rows in the 'deleted' virtual table.

I'm also confused by the WHERE clause in the INSERT statement:

WHERE del.MasterID = MastertId

Does the MastertId column exist in the 'deleted' virtual table? If so does MastertId ever equal del.MasterID?

Would the re-worked example below work for you?

Chris

ALTER TRIGGER trig_Audit_Upd

ON [dbo].[Master]

FOR UPDATE

--WITH ENCRYPTION

AS

IF EXISTS (SELECT 1 FROM deleted)

BEGIN

INSERT INTO [dbo].[syslogAuditMaster]

([MasterID],

[Name],

[CodeIDType],

[DateStarted],

[DateDue],

[CodeIDReminderType],

[CodeIDAuditStatus],

[NoteID],

[DeletedOnDate],

[Event])

SELECT del.[MasterID],

del.[Name],

del.[CodeIDType],

del.[Datestarted],

del.[Datedue],

del.[CodeIDReminderType],

del.[CodeIDAuditStatus],

del.[NoteID],

del.[DeletedOnDate],

'UPDATE'

FROM deleted del

END

|||

Thnaks for your replay,

I think you are missing the whole point of my question here ,which is to avoid the repetition in the Header or Master table when any update done against the Detailed table ..

I'm not sure if the "deleted and insered" tables hold a data record at the time or do they hold a set of records till the commit time .

it helped when I compared the "deleted " against the "inserted" data and by setting flag for any changes the insert to the Audit table was done.

although I'm not quite sure that is a good solution ,that I always think of using the functions will be more reliable and that was my resaon of asking about the Columns_Updated () and how far can I use it .

Thanks for your replies ..

|||

Just to clarify your requirement could you post examples of data in your Master table before and after the data has been updated, along with an example of the data that you would expect to be inserted into the syslogAuditMaster table as a result of the update?

Also if you could post the DDL statements used to create the Master and syslogAuditMaster tables then that would be extremely useful.

Thanks

Chris

|||

Hi Chris,

Thanks for your reply,

the DDL statements used to create the Master ,Detail and syslogAuditMaster are

--for the Master table

CREATE TABLE [dbo].[Master] (
[MasterID] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [nvarchar] (254) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[CodeIDType] [int] NOT NULL ,
[DateStarted] [datetime] NULL ,
[DateDue] [datetime] NULL ,
[CodeIDReminderType] [int] NULL ,
[CodeIDAuditStatus] [int] NULL ,
[NoteID] [int] NULL ,
[DeletedOnDate] [datetime] NULL
) ON [PRIMARY]

--for the Detail table

CREATE TABLE [dbo].[Detail] (
[DetailID] [int] IDENTITY (1, 1) NOT NULL ,
[MasterID] [int] NOT NULL ,
[Datestarted] [datetime] NULL , [NoteID] [int] NULL,[ColA] ...[ColB]..etc

) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Detail] ADD
CONSTRAINT [PK_Detail] PRIMARY KEY CLUSTERED
(
[DetailID]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[AuditDetail] ADD
CONSTRAINT [Master_Detail_FK1] FOREIGN KEY
(
[MasterID]
) REFERENCES [dbo].[Master] (
[MasterID]
)GO

-- for the Audit table

CREATE TABLE [dbo].[syslogAuditMaster] (
[MasterID] [int] NOT NULL,
[Name] [nvarchar] (254) NOT NULL ,
[CodeIDType] [int] NOT NULL ,
[DateStarted] [datetime] NULL ,
[DateDue] [datetime] NULL ,
[CodeIDReminderType] [int] NULL ,
[CodeIDAuditStatus] [int] NULL ,
[NoteID] [int] NULL ,
[DeletedOnDate] [datetime] NULL ,
[ChangedOnDate] [datetime] NOT NULL DEFAULT GETDATE(),
[UserName] [nvarchar] (150) NOT NULL DEFAULT suser_sname(),
[HostName] [nvarchar] (150) NOT NULL DEFAULT HOST_NAME(),
[Event] [nvarchar] (150) NOT NULL
) ON [PRIMARY]

when the user applied anychanges to the detail table through the .NET application , the store proc sp_Update which has the UPDATE statement for both the Master and Detail tables will executed,and as a result the DB trigget will be fired .

--Currently

if the field [NoteId] for [MasterID] = 3344 and [DetailID]=1122 has been changed in the Detail table ,I'll get the following in the syslogAuditMaster

[MasterID] = 3344,with the event type 'UPDATE'

--what to excpect

I should not get anything in the syslogAuditMaster ,cuz there is nothing new with the Master table to be added only if I changed anything within the Master I should get a record inserted in the AuditMaster

this is the DB trigger I'm using currently

Create Trigger trig_Audit_Upd
on [dbo].[Master]

For Update
--WITH ENCRYPTION
AS
IF EXISTS
(
SELECT 'True'
FROM deleted d inner join inserted i on i.MasterID = d.MasterID
where ltrim(rtrim(i.MasterID))+ltrim(rtrim(isnull(i.Name,'')))+ltrim(rtrim(isnull(i.CodeIDtype,'')))+ltrim(rtrim(isnull(i.DateStarted,'')))+ltrim(rtrim(isnull(i.DateDue,'')))+ltrim(rtrim(isnull(i.codeIDReminderType,'')))+ltrim(rtrim(isnull(i.CodeIDAuditStatus,'')))+ltrim(rtrim(isnull(i.NoteID,'')))+ltrim(rtrim(isnull(i.DeletedonDate,'')))
<> ltrim(rtrim(d.MasterID))+ltrim(rtrim(isnull(d.Name,'')))+ltrim(rtrim(isnull(d.CodeIDtype,'')))+ltrim(rtrim(isnull(d.DateStarted,'')))+ltrim(rtrim(isnull(d.DateDue,'')))+ltrim(rtrim(isnull(d.codeIDReminderType,'')))+ltrim(rtrim(isnull(d.CodeIDAuditStatus,'')))+ltrim(rtrim(isnull(d.NoteID,'')))+ltrim(rtrim(isnull(d.DeletedonDate,'')))
)
begin
INSERT INTO [dbo].[syslogAuditMaster](
[MasterID], [Name], [CodeIDType], [DateStarted], [DateDue], [CodeIDReminderType], [CodeIDAuditStatus], [NoteID], [DeletedOnDate], [Event])
Select del.[MasterID],del.[Name],del.[CodeIDType],del.[Datestarted],del.[Datedue],del.[CodeIDReminderType],
del.[CodeIDAuditStatus], del.[NoteID], del.[DeletedOnDate], 'UPDATE'
FROM deleted del
WHERE del.MasterID = MasterId

end;


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

hope this will help ..

Thank again

Ala'a M

|||

Right, I think I now understand where you are going with this.

Basically, even though you are issuing an UPDATE statement to update a row in the Master table, you might actually not be changing any of the values - in which case you don't want to write an audit row to the syslogAuditMaster table. You, therefore, only want to write a row to the syslogAuditMaster table if at least one of the columns values changes as a result of the update. Is this correct?

The problem with the COLUMNS_UPDATED() and UPDATED() functions is that they will report a column as being updated even if the column's value doesn't actually change as a result of the UPDATE statement. For this reason, if the solution you are using works fine then I see no reason why you shouldn't continue to use it.

Personally, I would move away from the text-based comparison that you are doing and would have explicit checks for each of the columns, see the example below. Note that there is no need for the IF EXISTS statement in this example. Also, if you update several Master rows at a time then the example below will only write audit rows for the Master rows that have actually changed.

Chris

CREATE TRIGGER trig_Audit_Upd

ON [dbo].[Master]

FOR UPDATE

--WITH ENCRYPTION

AS

INSERT INTO [dbo].[syslogAuditMaster](

[MasterID],

[Name],

[CodeIDType],

[DateStarted],

[DateDue],

[CodeIDReminderType],

[CodeIDAuditStatus],

[NoteID],

[DeletedOnDate],

[Event])

SELECT del.[MasterID],

del.[Name],

del.[CodeIDType],

del.[Datestarted],

del.[Datedue],

del.[CodeIDReminderType],

del.[CodeIDAuditStatus],

del.[NoteID],

del.[DeletedOnDate],

'UPDATE'

FROM deleted del

INNER JOIN inserted ins ON ins.MasterID = del.MasterID

WHERE ((del.[Name] <> ins.[Name])

OR (del.CodeIDType <> ins.CodeIDType)

OR ((del.[DateStarted] <> ins.[DateStarted]) OR (del.[DateStarted] IS NULL AND ins.[DateStarted] IS NOT NULL) OR (del.[DateStarted] IS NOT NULL AND ins.[DateStarted] IS NULL))

OR ((del.[DateDue] <> ins.[DateDue]) OR (del.[DateDue] IS NULL AND ins.[DateDue] IS NOT NULL) OR (del.[DateDue] IS NOT NULL AND ins.[DateDue] IS NULL))

OR ((del.[CodeIDReminderType] <> ins.[CodeIDReminderType]) OR (del.[CodeIDReminderType] IS NULL AND ins.[CodeIDReminderType] IS NOT NULL) OR (del.[CodeIDReminderType] IS NOT NULL AND ins.[CodeIDReminderType] IS NULL))

OR ((del.[CodeIDAuditStatus] <> ins.[CodeIDAuditStatus]) OR (del.[CodeIDAuditStatus] IS NULL AND ins.[CodeIDAuditStatus] IS NOT NULL) OR (del.[CodeIDAuditStatus] IS NOT NULL AND ins.[CodeIDAuditStatus] IS NULL))

OR ((del.[NoteID] <> ins.[NoteID]) OR (del.[NoteID] IS NULL AND ins.[NoteID] IS NOT NULL) OR (del.[NoteID] IS NOT NULL AND ins.[NoteID] IS NULL))

OR ((del.[DeletedOnDate] <> ins.[DeletedOnDate]) OR (del.[DeletedOnDate] IS NULL AND ins.[DeletedOnDate] IS NOT NULL) OR (del.[DeletedOnDate] IS NOT NULL AND ins.[DeletedOnDate] IS NULL)))

GO

|||

Thanks alot for giving me the chance to think loudly and exchange thoughts helped me alot ..

thanx Chris

Columns_Updated () and Update() functions

Hello all,

I'm trying to apply an audit DB trigger on a master-detail tables ,I do not need to insert record in the audit master every time an update happend in the audit detail,I tried to use columns_updated() > 0 but it didn't work the way I axpected it to be ..it stopped inserting into the audit master even if an update was applied against the master table ...any help please and I use the Update() function ? is their any major difference?

your help is appreciated

Thanks

Alaa M

COLUMNS_UPDATED() returns a value as a VARBINARY datatype, so for your comparison to work you need to convert the value to an INT first, like this:

CAST(COLUMNS_UPDATED() AS INT)

Chris

|||

Thanks CHRIS

Actually it didn't work ..

or may be i'm missing something here ,anyway this is the code I'm using if you or anyone can point the wrong thing that I'm doing here will be great.

this is the trigger for the master table ,and I only need to insert the changes applied against this table into Audit master no matter how many updates applied to the detail table.

ALTER Trigger trig_Audit_Upd
on [dbo].[Master]

For Update
--WITH ENCRYPTION
AS
IF EXISTS
(
SELECT 'True'
FROM deleted d
LEFT JOIN [Master] A
ON d.MasterID = A.MasterID
)

if (CAST(COLUMNS_UPDATED() AS INT ) & 255) > 0

begin
INSERT INTO [dbo].[syslogAuditMaster](
[MasterID], [Name], [CodeIDType], [DateStarted], [DateDue], [CodeIDReminderType],
[CodeIDAuditStatus], [NoteID], [DeletedOnDate], [Event])
Select del.[MasterID],del.[Name],del.[CodeIDType],del.[Datestarted],del.[Datedue],del.[CodeIDReminderType],
del.[CodeIDAuditStatus], del.[NoteID], del.[DeletedOnDate], 'UPDATE'
FROM deleted del
WHERE del.MasterID = MastertId

end;

thank

|||

I get the feeling that you might be trying to over-complicate things. I can't see the point of explicitly checking for updated columns using the COLUMNS_UPDATED() function - the fact that at least one row has been updated is implied by the existence of rows in the 'deleted' virtual table.

I'm also confused by the WHERE clause in the INSERT statement:

WHERE del.MasterID = MastertId

Does the MastertId column exist in the 'deleted' virtual table? If so does MastertId ever equal del.MasterID?

Would the re-worked example below work for you?

Chris

ALTER TRIGGER trig_Audit_Upd

ON [dbo].[Master]

FOR UPDATE

--WITH ENCRYPTION

AS

IF EXISTS (SELECT 1 FROM deleted)

BEGIN

INSERT INTO [dbo].[syslogAuditMaster]

([MasterID],

[Name],

[CodeIDType],

[DateStarted],

[DateDue],

[CodeIDReminderType],

[CodeIDAuditStatus],

[NoteID],

[DeletedOnDate],

[Event])

SELECT del.[MasterID],

del.[Name],

del.[CodeIDType],

del.[Datestarted],

del.[Datedue],

del.[CodeIDReminderType],

del.[CodeIDAuditStatus],

del.[NoteID],

del.[DeletedOnDate],

'UPDATE'

FROM deleted del

END

|||

Thnaks for your replay,

I think you are missing the whole point of my question here ,which is to avoid the repetition in the Header or Master table when any update done against the Detailed table ..

I'm not sure if the "deleted and insered" tables hold a data record at the time or do they hold a set of records till the commit time .

it helped when I compared the "deleted " against the "inserted" data and by setting flag for any changes the insert to the Audit table was done.

although I'm not quite sure that is a good solution ,that I always think of using the functions will be more reliable and that was my resaon of asking about the Columns_Updated () and how far can I use it .

Thanks for your replies ..

|||

Just to clarify your requirement could you post examples of data in your Master table before and after the data has been updated, along with an example of the data that you would expect to be inserted into the syslogAuditMaster table as a result of the update?

Also if you could post the DDL statements used to create the Master and syslogAuditMaster tables then that would be extremely useful.

Thanks

Chris

|||

Hi Chris,

Thanks for your reply,

the DDL statements used to create the Master ,Detail and syslogAuditMaster are

--for the Master table

CREATE TABLE [dbo].[Master] (
[MasterID] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [nvarchar] (254) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[CodeIDType] [int] NOT NULL ,
[DateStarted] [datetime] NULL ,
[DateDue] [datetime] NULL ,
[CodeIDReminderType] [int] NULL ,
[CodeIDAuditStatus] [int] NULL ,
[NoteID] [int] NULL ,
[DeletedOnDate] [datetime] NULL
) ON [PRIMARY]

--for the Detail table

CREATE TABLE [dbo].[Detail] (
[DetailID] [int] IDENTITY (1, 1) NOT NULL ,
[MasterID] [int] NOT NULL ,
[Datestarted] [datetime] NULL , [NoteID] [int] NULL,[ColA] ...[ColB]..etc

) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Detail] ADD
CONSTRAINT [PK_Detail] PRIMARY KEY CLUSTERED
(
[DetailID]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[AuditDetail] ADD
CONSTRAINT [Master_Detail_FK1] FOREIGN KEY
(
[MasterID]
) REFERENCES [dbo].[Master] (
[MasterID]
)GO

-- for the Audit table

CREATE TABLE [dbo].[syslogAuditMaster] (
[MasterID] [int] NOT NULL,
[Name] [nvarchar] (254) NOT NULL ,
[CodeIDType] [int] NOT NULL ,
[DateStarted] [datetime] NULL ,
[DateDue] [datetime] NULL ,
[CodeIDReminderType] [int] NULL ,
[CodeIDAuditStatus] [int] NULL ,
[NoteID] [int] NULL ,
[DeletedOnDate] [datetime] NULL ,
[ChangedOnDate] [datetime] NOT NULL DEFAULT GETDATE(),
[UserName] [nvarchar] (150) NOT NULL DEFAULT suser_sname(),
[HostName] [nvarchar] (150) NOT NULL DEFAULT HOST_NAME(),
[Event] [nvarchar] (150) NOT NULL
) ON [PRIMARY]

when the user applied anychanges to the detail table through the .NET application , the store proc sp_Update which has the UPDATE statement for both the Master and Detail tables will executed,and as a result the DB trigget will be fired .

--Currently

if the field [NoteId] for [MasterID] = 3344 and [DetailID]=1122 has been changed in the Detail table ,I'll get the following in the syslogAuditMaster

[MasterID] = 3344,with the event type 'UPDATE'

--what to excpect

I should not get anything in the syslogAuditMaster ,cuz there is nothing new with the Master table to be added only if I changed anything within the Master I should get a record inserted in the AuditMaster

this is the DB trigger I'm using currently

Create Trigger trig_Audit_Upd
on [dbo].[Master]

For Update
--WITH ENCRYPTION
AS
IF EXISTS
(
SELECT 'True'
FROM deleted d inner join inserted i on i.MasterID = d.MasterID
where ltrim(rtrim(i.MasterID))+ltrim(rtrim(isnull(i.Name,'')))+ltrim(rtrim(isnull(i.CodeIDtype,'')))+ltrim(rtrim(isnull(i.DateStarted,'')))+ltrim(rtrim(isnull(i.DateDue,'')))+ltrim(rtrim(isnull(i.codeIDReminderType,'')))+ltrim(rtrim(isnull(i.CodeIDAuditStatus,'')))+ltrim(rtrim(isnull(i.NoteID,'')))+ltrim(rtrim(isnull(i.DeletedonDate,'')))
<> ltrim(rtrim(d.MasterID))+ltrim(rtrim(isnull(d.Name,'')))+ltrim(rtrim(isnull(d.CodeIDtype,'')))+ltrim(rtrim(isnull(d.DateStarted,'')))+ltrim(rtrim(isnull(d.DateDue,'')))+ltrim(rtrim(isnull(d.codeIDReminderType,'')))+ltrim(rtrim(isnull(d.CodeIDAuditStatus,'')))+ltrim(rtrim(isnull(d.NoteID,'')))+ltrim(rtrim(isnull(d.DeletedonDate,'')))
)
begin
INSERT INTO [dbo].[syslogAuditMaster](
[MasterID], [Name], [CodeIDType], [DateStarted], [DateDue], [CodeIDReminderType], [CodeIDAuditStatus], [NoteID], [DeletedOnDate], [Event])
Select del.[MasterID],del.[Name],del.[CodeIDType],del.[Datestarted],del.[Datedue],del.[CodeIDReminderType],
del.[CodeIDAuditStatus], del.[NoteID], del.[DeletedOnDate], 'UPDATE'
FROM deleted del
WHERE del.MasterID = MasterId

end;


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

hope this will help ..

Thank again

Ala'a M

|||

Right, I think I now understand where you are going with this.

Basically, even though you are issuing an UPDATE statement to update a row in the Master table, you might actually not be changing any of the values - in which case you don't want to write an audit row to the syslogAuditMaster table. You, therefore, only want to write a row to the syslogAuditMaster table if at least one of the columns values changes as a result of the update. Is this correct?

The problem with the COLUMNS_UPDATED() and UPDATED() functions is that they will report a column as being updated even if the column's value doesn't actually change as a result of the UPDATE statement. For this reason, if the solution you are using works fine then I see no reason why you shouldn't continue to use it.

Personally, I would move away from the text-based comparison that you are doing and would have explicit checks for each of the columns, see the example below. Note that there is no need for the IF EXISTS statement in this example. Also, if you update several Master rows at a time then the example below will only write audit rows for the Master rows that have actually changed.

Chris

CREATE TRIGGER trig_Audit_Upd

ON [dbo].[Master]

FOR UPDATE

--WITH ENCRYPTION

AS

INSERT INTO [dbo].[syslogAuditMaster](

[MasterID],

[Name],

[CodeIDType],

[DateStarted],

[DateDue],

[CodeIDReminderType],

[CodeIDAuditStatus],

[NoteID],

[DeletedOnDate],

[Event])

SELECT del.[MasterID],

del.[Name],

del.[CodeIDType],

del.[Datestarted],

del.[Datedue],

del.[CodeIDReminderType],

del.[CodeIDAuditStatus],

del.[NoteID],

del.[DeletedOnDate],

'UPDATE'

FROM deleted del

INNER JOIN inserted ins ON ins.MasterID = del.MasterID

WHERE ((del.[Name] <> ins.[Name])

OR (del.CodeIDType <> ins.CodeIDType)

OR ((del.[DateStarted] <> ins.[DateStarted]) OR (del.[DateStarted] IS NULL AND ins.[DateStarted] IS NOT NULL) OR (del.[DateStarted] IS NOT NULL AND ins.[DateStarted] IS NULL))

OR ((del.[DateDue] <> ins.[DateDue]) OR (del.[DateDue] IS NULL AND ins.[DateDue] IS NOT NULL) OR (del.[DateDue] IS NOT NULL AND ins.[DateDue] IS NULL))

OR ((del.[CodeIDReminderType] <> ins.[CodeIDReminderType]) OR (del.[CodeIDReminderType] IS NULL AND ins.[CodeIDReminderType] IS NOT NULL) OR (del.[CodeIDReminderType] IS NOT NULL AND ins.[CodeIDReminderType] IS NULL))

OR ((del.[CodeIDAuditStatus] <> ins.[CodeIDAuditStatus]) OR (del.[CodeIDAuditStatus] IS NULL AND ins.[CodeIDAuditStatus] IS NOT NULL) OR (del.[CodeIDAuditStatus] IS NOT NULL AND ins.[CodeIDAuditStatus] IS NULL))

OR ((del.[NoteID] <> ins.[NoteID]) OR (del.[NoteID] IS NULL AND ins.[NoteID] IS NOT NULL) OR (del.[NoteID] IS NOT NULL AND ins.[NoteID] IS NULL))

OR ((del.[DeletedOnDate] <> ins.[DeletedOnDate]) OR (del.[DeletedOnDate] IS NULL AND ins.[DeletedOnDate] IS NOT NULL) OR (del.[DeletedOnDate] IS NOT NULL AND ins.[DeletedOnDate] IS NULL)))

GO

|||

Thanks alot for giving me the chance to think loudly and exchange thoughts helped me alot ..

thanx Chris

Thursday, March 8, 2012

Columns inster into problem

Hi Every1,
This is the first time I'm trying to use columns by themselves instead
of using individual values.
And I'm getting an error.
Here is my sql
INSERT INTO PS_CUST_CONTACT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS,
ADDRESS_SEQ_NUM, LAST_MAINT_OPRID, DATE_LAST_MAINT)
SELECT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
LAST_MAINT_OPRID, DATE_LAST_MAINT) FROM PS_CONTACT_CUST WHERE CUST_ID
NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
& the Error is
Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near ','.
I've matched all the columns. Second table have extra columns & I
didn't included them because then the columns will not match.
Also I only want to insert the values which are not currently present
in table 1.
Thanks in advance for your help.
Try...
INSERT INTO PS_CUST_CONTACT
SELECT SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
LAST_MAINT_OPRID, DATE_LAST_MAINT FROM PS_CONTACT_CUST WHERE CUST_ID
NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
Almost correct but you didn't need the brackets around the select columns,
and you don't need column names in the insert.
Peter
"You can always count on Americans to do the right thing - after they've
tried everything else."
Winston Churchill
"aspnetpal" wrote:

> Hi Every1,
> This is the first time I'm trying to use columns by themselves instead
> of using individual values.
> And I'm getting an error.
> Here is my sql
> INSERT INTO PS_CUST_CONTACT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS,
> ADDRESS_SEQ_NUM, LAST_MAINT_OPRID, DATE_LAST_MAINT)
> SELECT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
> LAST_MAINT_OPRID, DATE_LAST_MAINT) FROM PS_CONTACT_CUST WHERE CUST_ID
> NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
> & the Error is
> Server: Msg 170, Level 15, State 1, Line 2
> Line 2: Incorrect syntax near ','.
> I've matched all the columns. Second table have extra columns & I
> didn't included them because then the columns will not match.
> Also I only want to insert the values which are not currently present
> in table 1.
> Thanks in advance for your help.
>
|||Hello Peter, thanks for your suggestion.
After removing the brackets it worked just fine.
Thanks

Columns inster into problem

Hi Every1,
This is the first time I'm trying to use columns by themselves instead
of using individual values.
And I'm getting an error.
Here is my sql
INSERT INTO PS_CUST_CONTACT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS,
ADDRESS_SEQ_NUM, LAST_MAINT_OPRID, DATE_LAST_MAINT)
SELECT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
LAST_MAINT_OPRID, DATE_LAST_MAINT) FROM PS_CONTACT_CUST WHERE CUST_ID
NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
& the Error is
Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near ','.
I've matched all the columns. Second table have extra columns & I
didn't included them because then the columns will not match.
Also I only want to insert the values which are not currently present
in table 1.
Thanks in advance for your help.Try...
INSERT INTO PS_CUST_CONTACT
SELECT SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
LAST_MAINT_OPRID, DATE_LAST_MAINT FROM PS_CONTACT_CUST WHERE CUST_ID
NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
Almost correct but you didn't need the brackets around the select columns,
and you don't need column names in the insert.
Peter
"You can always count on Americans to do the right thing - after they've
tried everything else."
Winston Churchill
"aspnetpal" wrote:

> Hi Every1,
> This is the first time I'm trying to use columns by themselves instead
> of using individual values.
> And I'm getting an error.
> Here is my sql
> INSERT INTO PS_CUST_CONTACT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS,
> ADDRESS_SEQ_NUM, LAST_MAINT_OPRID, DATE_LAST_MAINT)
> SELECT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
> LAST_MAINT_OPRID, DATE_LAST_MAINT) FROM PS_CONTACT_CUST WHERE CUST_ID
> NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
> & the Error is
> Server: Msg 170, Level 15, State 1, Line 2
> Line 2: Incorrect syntax near ','.
> I've matched all the columns. Second table have extra columns & I
> didn't included them because then the columns will not match.
> Also I only want to insert the values which are not currently present
> in table 1.
> Thanks in advance for your help.
>|||Hello Peter, thanks for your suggestion.
After removing the brackets it worked just fine.
Thanks

Columns inster into problem

Hi Every1,
This is the first time I'm trying to use columns by themselves instead
of using individual values.
And I'm getting an error.
Here is my sql
INSERT INTO PS_CUST_CONTACT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS,
ADDRESS_SEQ_NUM, LAST_MAINT_OPRID, DATE_LAST_MAINT)
SELECT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
LAST_MAINT_OPRID, DATE_LAST_MAINT) FROM PS_CONTACT_CUST WHERE CUST_ID
NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
& the Error is
Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near ','.
I've matched all the columns. Second table have extra columns & I
didn't included them because then the columns will not match.
Also I only want to insert the values which are not currently present
in table 1.
Thanks in advance for your help.Try...
INSERT INTO PS_CUST_CONTACT
SELECT SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
LAST_MAINT_OPRID, DATE_LAST_MAINT FROM PS_CONTACT_CUST WHERE CUST_ID
NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
Almost correct but you didn't need the brackets around the select columns,
and you don't need column names in the insert.
Peter
"You can always count on Americans to do the right thing - after they've
tried everything else."
Winston Churchill
"aspnetpal" wrote:
> Hi Every1,
> This is the first time I'm trying to use columns by themselves instead
> of using individual values.
> And I'm getting an error.
> Here is my sql
> INSERT INTO PS_CUST_CONTACT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS,
> ADDRESS_SEQ_NUM, LAST_MAINT_OPRID, DATE_LAST_MAINT)
> SELECT (SETID, CUST_ID, CNTCT_SEQ_NUM, EFF_STATUS, ADDRESS_SEQ_NUM,
> LAST_MAINT_OPRID, DATE_LAST_MAINT) FROM PS_CONTACT_CUST WHERE CUST_ID
> NOT IN(SELECT CUST_ID FROM PS_CUST_CONTACT)
> & the Error is
> Server: Msg 170, Level 15, State 1, Line 2
> Line 2: Incorrect syntax near ','.
> I've matched all the columns. Second table have extra columns & I
> didn't included them because then the columns will not match.
> Also I only want to insert the values which are not currently present
> in table 1.
> Thanks in advance for your help.
>|||Hello Peter, thanks for your suggestion.
After removing the brackets it worked just fine.
Thanks

Wednesday, March 7, 2012

Column to store last time record was changed

In a table I have a column called ModifyDateTime which stores the last time
that record was modified. When a record is inserted this column is updated
through a default and when a record is updated a trigger is used. This is
all working fairly well except I would like the ModifyDateTime column to not
change if the data in the other columns is updated to the same thing.
Currently I need to make checks in my different place to ensure that an
update actually contains new data but if the trigger could do it then I'd
only need to check in one place.
Many thanks
Michael
eg
CREATE TABLE ABC(ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, SomeValue
NVARCHAR(5), ModifyDateTime DateTime NOT NULL DEFAULT GETDATE())
GO
CREATE TRIGGER [tgABC_Update] ON [dbo].[ABC]
FOR UPDATE
AS
UPDATE ABC SET ModifyDateTime = GETDATE() WHERE ID IN (SELECT ID FROM
INSERTED)
GO
INSERT INTO ABC (SomeValue) VALUES ('1')
SELECT * FROM ABC
--might need a delay here.
UPDATE ABC SET SomeValue = 1 --should not cause ModifyDateTime column to
change
SELECT * FROM ABC--BEGIN PGP SIGNED MESSAGE--
Hash: SHA1
IF Update(SomeValue)
RETURN
ELSE
UPDATE ABC SET ModifyDateTime = GETDATE() WHERE ID IN (SELECT ID FROM
inserted)
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
--BEGIN PGP SIGNATURE--
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBQlDEIYechKqOuFEgEQKR0gCg2CL/V2dejzcsa164lHtJV8pWGssAniBt
RTNymjjW6r627hdwwiZQBamQ
=bfki
--END PGP SIGNATURE--
Michael C wrote:
> In a table I have a column called ModifyDateTime which stores the last tim
e
> that record was modified. When a record is inserted this column is updated
> through a default and when a record is updated a trigger is used. This is
> all working fairly well except I would like the ModifyDateTime column to n
ot
> change if the data in the other columns is updated to the same thing.
> Currently I need to make checks in my different place to ensure that an
> update actually contains new data but if the trigger could do it then I'd
> only need to check in one place.
> Many thanks
> Michael
> eg
> CREATE TABLE ABC(ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, SomeValue
> NVARCHAR(5), ModifyDateTime DateTime NOT NULL DEFAULT GETDATE())
> GO
> CREATE TRIGGER [tgABC_Update] ON [dbo].[ABC]
> FOR UPDATE
> AS
> UPDATE ABC SET ModifyDateTime = GETDATE() WHERE ID IN (SELECT ID FROM
> INSERTED)
> GO
> INSERT INTO ABC (SomeValue) VALUES ('1')
> SELECT * FROM ABC
> --might need a delay here.
> UPDATE ABC SET SomeValue = 1 --should not cause ModifyDateTime column to
> change
> SELECT * FROM ABC
>
In your trigger use the IF Update(column) construct. E.g.:|||Michael
Look at IF UPDATE(column name) command to be used inside the trigger.
"Michael C" <mculley@.NOSPAMoptushome.com.au> wrote in message
news:e4$T%235MOFHA.3296@.TK2MSFTNGP15.phx.gbl...
> In a table I have a column called ModifyDateTime which stores the last
time
> that record was modified. When a record is inserted this column is updated
> through a default and when a record is updated a trigger is used. This is
> all working fairly well except I would like the ModifyDateTime column to
not
> change if the data in the other columns is updated to the same thing.
> Currently I need to make checks in my different place to ensure that an
> update actually contains new data but if the trigger could do it then I'd
> only need to check in one place.
> Many thanks
> Michael
> eg
> CREATE TABLE ABC(ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, SomeValue
> NVARCHAR(5), ModifyDateTime DateTime NOT NULL DEFAULT GETDATE())
> GO
> CREATE TRIGGER [tgABC_Update] ON [dbo].[ABC]
> FOR UPDATE
> AS
> UPDATE ABC SET ModifyDateTime = GETDATE() WHERE ID IN (SELECT ID FROM
> INSERTED)
> GO
> INSERT INTO ABC (SomeValue) VALUES ('1')
> SELECT * FROM ABC
> --might need a delay here.
> UPDATE ABC SET SomeValue = 1 --should not cause ModifyDateTime column to
> change
> SELECT * FROM ABC
>|||"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:%23c8snCNOFHA.3984@.TK2MSFTNGP12.phx.gbl...
> Michael
> Look at IF UPDATE(column name) command to be used inside the trigger.
Thanks Uri and MG,
IF Update(SomeValue)
RETURN
ELSE
UPDATE ABC SET ModifyDateTime = GETDATE() WHERE ID IN (SELECT ID FROM
inserted)
I'm presuming this won't work as I would like if the change was initiated
from an UPDATE statement and that causes only some of the data to change? eg
UPDATE ABC SET SomeValue = 'Z'
where half the rows already contain the value 'Z' in somefield.
I'm guessing the only method would be to use a cursor then to go through
each row. I can just use my existing method of checking everywhere I do an
update, which is probably the most efficient anyway because it never updates
the row at all.
Regards,
Michael|||Michael
Perhaps you need
UPDATE ABC SET SomeValue = 'Z' WHERE SomeValue <>'Z'
"Michael C" <mculley@.NOSPAMoptushome.com.au> wrote in message
news:OeDmtqNOFHA.3336@.TK2MSFTNGP09.phx.gbl...
> "Uri Dimant" <urid@.iscar.co.il> wrote in message
> news:%23c8snCNOFHA.3984@.TK2MSFTNGP12.phx.gbl...
> Thanks Uri and MG,
> IF Update(SomeValue)
> RETURN
> ELSE
> UPDATE ABC SET ModifyDateTime = GETDATE() WHERE ID IN (SELECT ID FROM
> inserted)
> I'm presuming this won't work as I would like if the change was initiated
> from an UPDATE statement and that causes only some of the data to change?
eg
> UPDATE ABC SET SomeValue = 'Z'
> where half the rows already contain the value 'Z' in somefield.
> I'm guessing the only method would be to use a cursor then to go through
> each row. I can just use my existing method of checking everywhere I do an
> update, which is probably the most efficient anyway because it never
updates
> the row at all.
> Regards,
> Michael
>|||On Mon, 4 Apr 2005 15:59:34 +1000, Michael C wrote:

>"Uri Dimant" <urid@.iscar.co.il> wrote in message
>news:%23c8snCNOFHA.3984@.TK2MSFTNGP12.phx.gbl...
>Thanks Uri and MG,
>IF Update(SomeValue)
> RETURN
>ELSE
> UPDATE ABC SET ModifyDateTime = GETDATE() WHERE ID IN (SELECT ID FROM
>inserted)
>I'm presuming this won't work as I would like if the change was initiated
>from an UPDATE statement and that causes only some of the data to change? e
g
>UPDATE ABC SET SomeValue = 'Z'
>where half the rows already contain the value 'Z' in somefield.
>I'm guessing the only method would be to use a cursor then to go through
>each row. I can just use my existing method of checking everywhere I do an
>update, which is probably the most efficient anyway because it never update
s
>the row at all.
Hi Michael,
Oh no, there absolutely no need to slow things down by using a cursor;
this can easily be accomplished with setbased instructions:
IF UPDATE(SomeColumn) -- Saves time if the colummn's not updated at all
UPDATE ABC
SET ModifyDateTime = CURRRENT_TIMESTAMP
WHERE EXISTS
(SELECT *
FROM deleted AS d -- Holds old vales (before update)
WHERE d.KeyCol1 = ABC.KeyCol1 -- Repeat for all columns that
AND d.KeyCol2 = ABD.KeyCol2 -- make up the primary key
AND d.SomeColumn <> ABC.SomeColumn) -- Actually changed?
The above assumes that SomeColumn will never be NULL. If it can be NULL,
you'll have to change the last line. Either like this, using a value
that will never be really used in the column:
AND COALESCE(d.SomeColumn, -123)
<> COALESCE(ABC.SomeColumn, -123)
Or, if you don't have any value that will never be in the column (or
prefer not to rely on such assumptions), you can use this not very
intuuitive but very reliable version:
AND NULLIF(d.SomeColumn, ABC.SomeColumn) IS NULL
AND NULLIF(ABC.SomeColumn, d.SomeColumn) IS NULL
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:uZAnIEOOFHA.2936@.TK2MSFTNGP10.phx.gbl...
> Michael
> Perhaps you need
> UPDATE ABC SET SomeValue = 'Z' WHERE SomeValue <>'Z'
Thanks Uri. Unfortunately, that is what I'm trying to avoid. I have a few
statements like what you've written here that update the same table but they
are much more complicated, if the trigger does the checking then I will only
need the check in one central location. In the end it is not really that
important and I might do it either way, or even both for efficiency (as your
statement updates less records).
Regards,
Michael|||"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:3n5351pi5ggb1dov6qqej33k9dq9esnvnm@.
4ax.com...
> On Mon, 4 Apr 2005 15:59:34 +1000, Michael C wrote:
Thank Hugo, I'll have a look at it now. I really like the NULLIF idea, i've
been using
ISNULL(X, '{00000000-0000-0000-0000-00000000}') = ISNULL(Y,
'{00000000-0000-0000-0000-00000000}')
for guids, which I've always thought could be simpler.
Michael|||"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:3n5351pi5ggb1dov6qqej33k9dq9esnvnm@.
4ax.com...
> Oh no, there absolutely no need to slow things down by using a cursor;
> this can easily be accomplished with setbased instructions:
Thanks Hugo, that worked perfectly after lots of playing around. I had a
couple of questions if you don't mind. Is it preferrable to use
CURRENT_TIMESTAMP instead of GETDATE()? Ditto for EXISTS instead of an IN
clause?

> AND NULLIF(d.SomeColumn, ABC.SomeColumn) IS NULL
> AND NULLIF(ABC.SomeColumn, d.SomeColumn) IS NULL
Is this something they overlooked in SQLServer? Seems to me there should
something built in for comparing 2 values that you want to consider equal
when both are null.
Thanks,
Michael|||On Tue, 5 Apr 2005 14:20:32 +1000, Michael C wrote:
(snip)
> I had a
>couple of questions if you don't mind.
Hi Michael,
I don't mind :-)

> Is it preferrable to use
>CURRENT_TIMESTAMP instead of GETDATE()?
GETDATE() is the name chosen for the function in Sybase and SQL Server
before there were any standards to adhere to. The ANSI standard settled
for CURRENT_TIMESTAMP. Since either 7.0 or 2000, both are recognised by
SQL Server. Many people still use GETDATE (I do - old habits die hard),
but for portability, CURRENT_TIMESTAMP is prefered.

> Ditto for EXISTS instead of an IN
>clause?
I prefer to use IN only with a list of constants (as a shorthand for a
range of expressions OR'ed together), and EXISTS in all other cases. Not
for portability (both conform to ANSI standard), not for performance
(though I've read reports claiming that EXISTS is often faster), but for
another reason.
If you combine the IN operator with NOT (i.e. NOT IN), and the subquery
after NOT IN returns one or more NULL values, then the NOT IN operator
will never evaluate to true. This surprises lots of people, because many
people have trouble understanding the role NULL plays in the relational
model. What would you answer if I asked you if the number 8 is IN
(current monthnumber, age of my daughter, #days per w)?

>Is this something they overlooked in SQLServer? Seems to me there should
>something built in for comparing 2 values that you want to consider equal
>when both are null.
This is not an oversight, it's a feature (and no, this is not intended
as a cynical remark, I'm serious). NULL means that the data is missing,
not available, unknown. I don't think that you should ever want a
database to conclude that two unknown values are equal. Is my daughter's
age equal to the age of the girl living next door to me?
And even if it is an oversight, then definitely not in SQL Server. In
this regard, SQL Server strictly adheres to the ANSI standard handling
of NULL.
Besides, there IS a way to compare 2 values and consider them equal of
both are NULL. Several ways, in fact. You can use a combination of two
NULLIF expressions like the one above. Or you can use COALESCE to
replace NULL with a value that won't ever appear in regular data, then
compare the resutls. Or you could use an expression such as "col1 = col2
OR (col1 IS NULL AND col2 IS NULL)"
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

Friday, February 24, 2012

column info from temp table

Hello,
i have a stored proc where i create two temp tables with complete equal
structure. But the structure is different every time the stored proc is
called. Now i want to find all records in temp table 1 which are not in
temp table 2.
I am not sure if the sorting in both tables is equal. So i fear that i have
to compare one record from one table with all records from the other table
field by field.
How can i do this? Or is there a better solution?
thanks,
HelmutIf you are using SQL Server 2005 you can use except operator to compare
tables.
Regards
Amish Shah|||Hi, Helmut
Temporary tables are stored in tempdb.
You can find the columns of temporary tables like this:
CREATE TABLE #T1 (X INT, Y INT)
SELECT c.name FROM tempdb.dbo.sysobjects o
INNER JOIN tempdb.dbo.syscolumns c ON c.id=o.id
WHERE o.name LIKE '#T1%'
However, if tables with the same name are created by two different
processes at the same time you need to consider also the suffix of the
table, which is different for each process (is based on the "connection
number", i.e. the value of @.@.CONNECTION when the connection is made;
also, a part of the suffix is based on @.@.NESTLEVEL). I do not know any
reliable method of determining this suffix in a live environment
(because the value of @.@.CONNECTION may have been changed since the
beginning of the connection).
Razvan|||Helmut,
As you say, the structure may be different each call bot the tables are
identical, so you may well know the likely PK available.
Apart from that point, it may be useful to know what leads to you populating
two tables in any case. Why can you not take the comparison back one step an
d
attempt the comparison there?
If you want to reveal the basic SProc code here it may well be a lot clearer
to assist you,
Tony
"helmut woess" wrote:

> Hello,
> i have a stored proc where i create two temp tables with complete equal
> structure. But the structure is different every time the stored proc is
> called. Now i want to find all records in temp table 1 which are not in
> temp table 2.
> I am not sure if the sorting in both tables is equal. So i fear that i hav
e
> to compare one record from one table with all records from the other table
> field by field.
> How can i do this? Or is there a better solution?
> thanks,
> Helmut
>|||Am 3 Feb 2006 06:48:52 -0800 schrieb amish:

> If you are using SQL Server 2005 you can use except operator to compare
> tables.
> Regards
> Amish Shah
Arrrrgghhh, i can't use SQL 2005, i am bound to SQL 2000 :-(
But good to know that in future this problem not longer exists.
thanks,
Helmut|||Hi, Razvan
thank you for this info, i will see if i can find out more about it. But it
shed some light in my problem :-)
Helmut|||Am Fri, 3 Feb 2006 07:11:07 -0800 schrieb Tony Scott:
...
> Apart from that point, it may be useful to know what leads to you populati
ng
> two tables in any case. Why can you not take the comparison back one step
and
> attempt the comparison there?
The starting point you can see if you look some entries down, it is the
problem building the Oracle MINUS functionality for SQL Server 2000.
I am short before finishing a first solution and then i will post it here
in hope that it will become much more clearer what i need and maybe some
people can help me to make it more universal, more stable, ... and much
more faster :-)
bye,
Helmut|||"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1138979281.680288.12130@.o13g2000cwo.googlegroups.com...
> Hi, Helmut
> Temporary tables are stored in tempdb.
> You can find the columns of temporary tables like this:
> CREATE TABLE #T1 (X INT, Y INT)
> SELECT c.name FROM tempdb.dbo.sysobjects o
> INNER JOIN tempdb.dbo.syscolumns c ON c.id=o.id
> WHERE o.name LIKE '#T1%'
> However, if tables with the same name are created by two different
> processes at the same time you need to consider also the suffix of the
> table, which is different for each process (is based on the "connection
> number", i.e. the value of @.@.CONNECTION when the connection is made;
> also, a part of the suffix is based on @.@.NESTLEVEL). I do not know any
> reliable method of determining this suffix in a live environment
> (because the value of @.@.CONNECTION may have been changed since the
> beginning of the connection).
> Razvan
or you can use object_id() function:
SELECT c.name FROM tempdb.dbo.sysobjects o
INNER JOIN tempdb.dbo.syscolumns c ON c.id=o.id
WHERE o.id=object_id('tempdb..#T1')
dean|||Hi, Dean
You are right. It's much better this way, since it eliminates the
problem of multiple temporary tables with the same name.
Razvan|||Am Fri, 3 Feb 2006 19:28:16 +0100 schrieb Dean:

> SELECT c.name FROM tempdb.dbo.sysobjects o
> INNER JOIN tempdb.dbo.syscolumns c ON c.id=o.id
> WHERE o.id=object_id('tempdb..#T1')
Oh yes, perfect.
thanks,
Helmut

Sunday, February 12, 2012

Collations ... blah!

What collation setting(s) do I need to set with RebuildM to get the
SQL_Latin1_General_CP1_CI_AS collation?
3rd time going and I can't seem to set/select the right configuration.
Thanks,
MorganFound it...
Dictionary Order, case insensitive for use with 1252 Char Set.
Thanks,
Morgan
"Morgan" <mfears@.spamcop.net> wrote in message
news:ueb0z8jxDHA.2328@.TK2MSFTNGP10.phx.gbl...
> What collation setting(s) do I need to set with RebuildM to get the
> SQL_Latin1_General_CP1_CI_AS collation?
> 3rd time going and I can't seem to set/select the right configuration.
>
> Thanks,
> Morgan
>

Friday, February 10, 2012

Collation Properties

Hi,
I want to specify case sensitivity for my database, but at the same time i
want let customer decide the sort order.
What i mean is: take the following collation property
SQL_Latin1_general_cs_as
in the above property, i want to specify only CS, and let cusotmer decide
what to (example: what sort order) specify.
Is it possible? How to do this? I hope this is a very natural requirement,
but could find help in MS documents for this
Thanks,
Venkat
The collation determines the sort rules, so you cannot specify a Collation
and apply different sort rules.
Consider two other important issues:
- a collation applied to a single database involves problems regarding the
collation of the other databases of the same instance (eg. master, msdb, etc).
To resolve this issue is better apply a specific collation to the table
columns (very tedious but...).
the Latin1_general_cs_as sorts upper case and lower case separately but in a
different way among Latin1_General_BIN;I don't remember exactly which one,
but one sorts all the upper cases together separately among the lower case,
the other sorts instead A with a, B with b and so on.
Remeber furthermore tha the new collation does'nt convert the collation of
data already present in your tables.
"Venkat" wrote:

> Hi,
> I want to specify case sensitivity for my database, but at the same time i
> want let customer decide the sort order.
> What i mean is: take the following collation property
> SQL_Latin1_general_cs_as
> in the above property, i want to specify only CS, and let cusotmer decide
> what to (example: what sort order) specify.
> Is it possible? How to do this? I hope this is a very natural requirement,
> but could find help in MS documents for this
> Thanks,
> Venkat
>
>
|||Hi,
I would like to understand your statement:
" collation applied to a single database involves problems regarding the
collation of the other databases of the same instance (eg. master, msdb,
etc)."
What is the problem if my database is in diffrent collaiton than the master
database?
I am sure this is possible and there would be no issuees with this.
There might be issues with tempdb, so you can specify collation when you are
creating temp tables.
Can you tell me what is the problems you are expecting if master database
and user database is in diffrent collation?
Other thing you said is: sorting, case sensitivity, accent sensitivity (I
call them as collaiton properties) have to be specified together only?
the reason you said is: all these properties together forms as sort rules?
If so, then when the properties: Latin1, General going to be used and what
purpose they are for? (asume that i have Unicode database)
comments?
Regards,
Venkat
"Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
message news:7E4D942B-9CEE-46E8-805B-F64233738B04@.microsoft.com...[vbcol=seagreen]
> The collation determines the sort rules, so you cannot specify a Collation
> and apply different sort rules.
> Consider two other important issues:
> - a collation applied to a single database involves problems regarding the
> collation of the other databases of the same instance (eg. master, msdb,
> etc).
> To resolve this issue is better apply a specific collation to the table
> columns (very tedious but...).
> the Latin1_general_cs_as sorts upper case and lower case separately but in
> a
> different way among Latin1_General_BIN;I don't remember exactly which one,
> but one sorts all the upper cases together separately among the lower
> case,
> the other sorts instead A with a, B with b and so on.
> Remeber furthermore tha the new collation does'nt convert the collation
> of
> data already present in your tables.
>
> "Venkat" wrote:
|||Hi,
i wrote about issues in generic terms: having objects in different
collations COULD be an issue, but not necessarily IS. Surely some (very
little) overhead due to internal conversion is more probable in presence of
more than one collation.
More: the sort order is implicit in the collation you select: BIN (binary)
sort is different among CS_AS or CI_AS or CI_AI etc.
So the Properties Latin1, General are "complementary" with the CS, CI, AS,
AI, KS (etc.) properties: the sort order could be different among each
available combination.
The concept of collation involves not only sort rules but also character set
consideration.
You cannot define o sort order apart from the selected collation.
"Venkat" wrote:

> Hi,
> I would like to understand your statement:
> " collation applied to a single database involves problems regarding the
> collation of the other databases of the same instance (eg. master, msdb,
> etc)."
> What is the problem if my database is in diffrent collaiton than the master
> database?
> I am sure this is possible and there would be no issuees with this.
> There might be issues with tempdb, so you can specify collation when you are
> creating temp tables.
> Can you tell me what is the problems you are expecting if master database
> and user database is in diffrent collation?
> Other thing you said is: sorting, case sensitivity, accent sensitivity (I
> call them as collaiton properties) have to be specified together only?
> the reason you said is: all these properties together forms as sort rules?
> If so, then when the properties: Latin1, General going to be used and what
> purpose they are for? (asume that i have Unicode database)
> comments?
> Regards,
> Venkat
> "Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
> message news:7E4D942B-9CEE-46E8-805B-F64233738B04@.microsoft.com...
>
>
|||Hi,
I would like to restate what you said about "complimentary" properties like
Latin, General etc.,
As per my understanding these complimentary properties are useful for non
unicode database.
Beucase as per my guess these properties (Latin, General etc.,) specifies
how the data can be stored: in which character set for non unicode database.
Where as these complimnetary properties are of no use (but we still need to
specify) for unicode database.
You have any comments on my understanding?
Regards,
Venkat
"Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
message news:BF5A2E30-5D28-4129-B025-487DB4F6949D@.microsoft.com...[vbcol=seagreen]
> Hi,
> i wrote about issues in generic terms: having objects in different
> collations COULD be an issue, but not necessarily IS. Surely some (very
> little) overhead due to internal conversion is more probable in presence
> of
> more than one collation.
> More: the sort order is implicit in the collation you select: BIN (binary)
> sort is different among CS_AS or CI_AS or CI_AI etc.
> So the Properties Latin1, General are "complementary" with the CS, CI, AS,
> AI, KS (etc.) properties: the sort order could be different among each
> available combination.
> The concept of collation involves not only sort rules but also character
> set
> consideration.
> You cannot define o sort order apart from the selected collation.
>
> "Venkat" wrote:
|||No comments about your understanding.
The question is:
Apart (in unicode evironment) from "localizations" given by the different
available collations (Latin, Japanese, etc), the sort rules are different
between CS, CI, AS, AI,or any combination of them you'll like to select and
the BIN option: anyway, you can make just one choice: so you will provide a
"binary" sort criteria or a case, accent and/or katana sensitivity (or
insensitivity) sorto criteria, depending on the collation you'll set for your
Server, database or column
Regards
Gilberto
"Venkat" wrote:

> Hi,
> I would like to restate what you said about "complimentary" properties like
> Latin, General etc.,
> As per my understanding these complimentary properties are useful for non
> unicode database.
> Beucase as per my guess these properties (Latin, General etc.,) specifies
> how the data can be stored: in which character set for non unicode database.
> Where as these complimnetary properties are of no use (but we still need to
> specify) for unicode database.
> You have any comments on my understanding?
> Regards,
> Venkat
> "Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
> message news:BF5A2E30-5D28-4129-B025-487DB4F6949D@.microsoft.com...
>
>

Collation Properties

Hi,
I want to specify case sensitivity for my database, but at the same time i
want let customer decide the sort order.
What i mean is: take the following collation property
SQL_Latin1_general_cs_as
in the above property, i want to specify only CS, and let cusotmer decide
what to (example: what sort order) specify.
Is it possible? How to do this? I hope this is a very natural requirement,
but could find help in MS documents for this
Thanks,
VenkatThe collation determines the sort rules, so you cannot specify a Collation
and apply different sort rules.
Consider two other important issues:
- a collation applied to a single database involves problems regarding the
collation of the other databases of the same instance (eg. master, msdb, etc
).
To resolve this issue is better apply a specific collation to the table
columns (very tedious but...).
the Latin1_general_cs_as sorts upper case and lower case separately but in a
different way among Latin1_General_BIN;I don't remember exactly which one,
but one sorts all the upper cases together separately among the lower case,
the other sorts instead A with a, B with b and so on.
Remeber furthermore tha the new collation does'nt convert the collation of
data already present in your tables.
"Venkat" wrote:

> Hi,
> I want to specify case sensitivity for my database, but at the same time i
> want let customer decide the sort order.
> What i mean is: take the following collation property
> SQL_Latin1_general_cs_as
> in the above property, i want to specify only CS, and let cusotmer decide
> what to (example: what sort order) specify.
> Is it possible? How to do this? I hope this is a very natural requirement,
> but could find help in MS documents for this
> Thanks,
> Venkat
>
>|||Hi,
I would like to understand your statement:
" collation applied to a single database involves problems regarding the
collation of the other databases of the same instance (eg. master, msdb,
etc)."
What is the problem if my database is in diffrent collaiton than the master
database?
I am sure this is possible and there would be no issuees with this.
There might be issues with tempdb, so you can specify collation when you are
creating temp tables.
Can you tell me what is the problems you are expecting if master database
and user database is in diffrent collation?
Other thing you said is: sorting, case sensitivity, accent sensitivity (I
call them as collaiton properties) have to be specified together only?
the reason you said is: all these properties together forms as sort rules?
If so, then when the properties: Latin1, General going to be used and what
purpose they are for? (asume that i have Unicode database)
comments?
Regards,
Venkat
"Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
message news:7E4D942B-9CEE-46E8-805B-F64233738B04@.microsoft.com...[vbcol=seagreen]
> The collation determines the sort rules, so you cannot specify a Collation
> and apply different sort rules.
> Consider two other important issues:
> - a collation applied to a single database involves problems regarding the
> collation of the other databases of the same instance (eg. master, msdb,
> etc).
> To resolve this issue is better apply a specific collation to the table
> columns (very tedious but...).
> the Latin1_general_cs_as sorts upper case and lower case separately but in
> a
> different way among Latin1_General_BIN;I don't remember exactly which one,
> but one sorts all the upper cases together separately among the lower
> case,
> the other sorts instead A with a, B with b and so on.
> Remeber furthermore tha the new collation does'nt convert the collation
> of
> data already present in your tables.
>
> "Venkat" wrote:
>|||Hi,
i wrote about issues in generic terms: having objects in different
collations COULD be an issue, but not necessarily IS. Surely some (very
little) overhead due to internal conversion is more probable in presence of
more than one collation.
More: the sort order is implicit in the collation you select: BIN (binary)
sort is different among CS_AS or CI_AS or CI_AI etc.
So the Properties Latin1, General are "complementary" with the CS, CI, AS,
AI, KS (etc.) properties: the sort order could be different among each
available combination.
The concept of collation involves not only sort rules but also character set
consideration.
You cannot define o sort order apart from the selected collation.
"Venkat" wrote:

> Hi,
> I would like to understand your statement:
> " collation applied to a single database involves problems regarding the
> collation of the other databases of the same instance (eg. master, msdb,
> etc)."
> What is the problem if my database is in diffrent collaiton than the maste
r
> database?
> I am sure this is possible and there would be no issuees with this.
> There might be issues with tempdb, so you can specify collation when you a
re
> creating temp tables.
> Can you tell me what is the problems you are expecting if master database
> and user database is in diffrent collation?
> Other thing you said is: sorting, case sensitivity, accent sensitivity (I
> call them as collaiton properties) have to be specified together only?
> the reason you said is: all these properties together forms as sort rules?
> If so, then when the properties: Latin1, General going to be used and what
> purpose they are for? (asume that i have Unicode database)
> comments?
> Regards,
> Venkat
> "Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
> message news:7E4D942B-9CEE-46E8-805B-F64233738B04@.microsoft.com...
>
>|||Hi,
I would like to restate what you said about "complimentary" properties like
Latin, General etc.,
As per my understanding these complimentary properties are useful for non
unicode database.
Beucase as per my guess these properties (Latin, General etc.,) specifies
how the data can be stored: in which character set for non unicode database.
Where as these complimnetary properties are of no use (but we still need to
specify) for unicode database.
You have any comments on my understanding?
Regards,
Venkat
"Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
message news:BF5A2E30-5D28-4129-B025-487DB4F6949D@.microsoft.com...[vbcol=seagreen]
> Hi,
> i wrote about issues in generic terms: having objects in different
> collations COULD be an issue, but not necessarily IS. Surely some (very
> little) overhead due to internal conversion is more probable in presence
> of
> more than one collation.
> More: the sort order is implicit in the collation you select: BIN (binary)
> sort is different among CS_AS or CI_AS or CI_AI etc.
> So the Properties Latin1, General are "complementary" with the CS, CI, AS,
> AI, KS (etc.) properties: the sort order could be different among each
> available combination.
> The concept of collation involves not only sort rules but also character
> set
> consideration.
> You cannot define o sort order apart from the selected collation.
>
> "Venkat" wrote:
>|||No comments about your understanding.
The question is:
Apart (in unicode evironment) from "localizations" given by the different
available collations (Latin, Japanese, etc), the sort rules are different
between CS, CI, AS, AI,or any combination of them you'll like to select and
the BIN option: anyway, you can make just one choice: so you will provide a
"binary" sort criteria or a case, accent and/or katana sensitivity (or
insensitivity) sorto criteria, depending on the collation you'll set for you
r
Server, database or column
Regards
Gilberto
"Venkat" wrote:

> Hi,
> I would like to restate what you said about "complimentary" properties lik
e
> Latin, General etc.,
> As per my understanding these complimentary properties are useful for non
> unicode database.
> Beucase as per my guess these properties (Latin, General etc.,) specifies
> how the data can be stored: in which character set for non unicode databas
e.
> Where as these complimnetary properties are of no use (but we still need t
o
> specify) for unicode database.
> You have any comments on my understanding?
> Regards,
> Venkat
> "Gilberto Zampatti" <GilbertoZampatti@.discussions.microsoft.com> wrote in
> message news:BF5A2E30-5D28-4129-B025-487DB4F6949D@.microsoft.com...
>
>