Showing posts with label columns. Show all posts
Showing posts with label columns. Show all posts

Monday, March 19, 2012

"Update" in Trigger not working

Hi,

I am trying to concatenate the columns (PrevEmp01, PrevEmp02, PrevEmp03, PrevEmp04, PrevEmp05) into column (ft) using trigger:

CREATE TRIGGER [tg_prevemp_ft_update] ON [tStaffDir_PrevEmp]
FOR INSERT, UPDATE
AS
UPDATE tStaffDir_PrevEmp SET ft = PrevEmp01 + ' ' + PrevEmp02 + ' ' + PrevEmp03 + ' ' + PrevEmp04 + ' ' + PrevEmp05

I would expect the (ft) column will be populated accordingly regardless if any of the columns are (Null).
But the Trigger will only work when all the 5 columns are populated. If one of the column is (Null), the (ft) column will be (Null) too.

Please advise. Many Thanks.

Try this:

CREATE TRIGGER [tg_prevemp_ft_update]ON [tStaffDir_PrevEmp]FOR INSERT,UPDATEASUPDATE tStaffDir_PrevEmpSET ft =ISNULL(PrevEmp01,'') +' ' +ISNULL(PrevEmp02,'') +' ' +ISNULL(PrevEmp03,'') +' ' +ISNULL(PrevEmp04,'') +' ' +ISNULL(PrevEmp05,'')

Good luck.

|||

Thanks! It worked.

Friday, March 16, 2012

"SUM" of a varchar column?

I have a table that contains a number of columns containing either a varchar
value, or null. There are multiple rows per grouping. I would like to colapse
the multiple rows into a single row, either by appending the strings to each
other, or simply selecting the first (or last) one.
If it was a number field I could do a SUM, MAX or MIN. SUM doesn't work, for
obvious reasons. I thought MAX would work, but MAX returns only one value for
all of the columns (as opposed to one per column) and the rest are left as
null.
Any suggestions?
Here's an example...
The table contains this data, all items are varchar
32611317
3261Non-Client
32612
3261mmarkowitz
I'd like to turn this into...
3261 1317 Non-client 2 mmarkowitz
|||This looks like a PIVOT. Can you flatten this table out on the client? If
not, see http://www.aspfaq.com/2462
http://www.aspfaq.com/
(Reverse address to reply.)
"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:7838E3D4-CD8D-46EB-A75F-D069B30EE9AB@.microsoft.com...
> I have a table that contains a number of columns containing either a
varchar
> value, or null. There are multiple rows per grouping. I would like to
colapse
> the multiple rows into a single row, either by appending the strings to
each
> other, or simply selecting the first (or last) one.
> If it was a number field I could do a SUM, MAX or MIN. SUM doesn't work,
for
> obvious reasons. I thought MAX would work, but MAX returns only one value
for
> all of the columns (as opposed to one per column) and the rest are left as
> null.
> Any suggestions?
|||"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:DD2FA7A5-A0F2-4BFF-85E9-42533F4E423C@.microsoft.com...
> Here's an example...
> The table contains this data, all items are varchar
>
> 3261 1317
> 3261 Non-Client
> 3261 2
> 3261 mmarkowitz
> I'd like to turn this into...
> 3261 1317 Non-client 2 mmarkowitz
I'm not sure if this will help, but you may want to take a look at the
GROUP BY WITH ROLLUP and WITH CUBE commands. It may work for what you are
after.
SELECT Col1, Max(Col2)
FROM tablename
GROUP BY Col1
WITH ROLLUP
HTH
Rick Sawtell
MCT, MCSD, MCDBA
|||SELECT col1, MAX(col2), MAX(col3), MAX(col4), ...
FROM YourTable
GROUP BY col1
David Portas
SQL Server MVP
|||I don't think this will work, he wants to flatten one ofthe two columns in
his table out into multiple columns...
http://www.aspfaq.com/
(Reverse address to reply.)
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1103306604.369012.58920@.z14g2000cwz.googlegro ups.com...
> SELECT col1, MAX(col2), MAX(col3), MAX(col4), ...
> FROM YourTable
> GROUP BY col1
> --
> David Portas
> SQL Server MVP
> --
>
|||OK. Looks to me like 5 columns but I guess that's just presentational ;-)
Tough to pivot without an explicit attribute for the column.
David Portas
SQL Server MVP
|||"Aaron [SQL Server MVP]" wrote:

> This looks like a PIVOT.
Actually it is the RESULT of a pivot, which is why it is spread out
vertically like that.
But I did figure out a "trick". After reading the page you sent, I combined
their technique of ISNULL (instead of CASE) with MIN, and presto.
Thanks!
(anyone interested in the code?)

"SUM" of a varchar column?

I have a table that contains a number of columns containing either a varchar
value, or null. There are multiple rows per grouping. I would like to colapse
the multiple rows into a single row, either by appending the strings to each
other, or simply selecting the first (or last) one.
If it was a number field I could do a SUM, MAX or MIN. SUM doesn't work, for
obvious reasons. I thought MAX would work, but MAX returns only one value for
all of the columns (as opposed to one per column) and the rest are left as
null.
Any suggestions?Here's an example...
The table contains this data, all items are varchar
3261 1317
3261 Non-Client
3261 2
3261 mmarkowitz
I'd like to turn this into...
3261 1317 Non-client 2 mmarkowitz|||This looks like a PIVOT. Can you flatten this table out on the client? If
not, see http://www.aspfaq.com/2462
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:7838E3D4-CD8D-46EB-A75F-D069B30EE9AB@.microsoft.com...
> I have a table that contains a number of columns containing either a
varchar
> value, or null. There are multiple rows per grouping. I would like to
colapse
> the multiple rows into a single row, either by appending the strings to
each
> other, or simply selecting the first (or last) one.
> If it was a number field I could do a SUM, MAX or MIN. SUM doesn't work,
for
> obvious reasons. I thought MAX would work, but MAX returns only one value
for
> all of the columns (as opposed to one per column) and the rest are left as
> null.
> Any suggestions?|||"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:DD2FA7A5-A0F2-4BFF-85E9-42533F4E423C@.microsoft.com...
> Here's an example...
> The table contains this data, all items are varchar
>
> 3261 1317
> 3261 Non-Client
> 3261 2
> 3261 mmarkowitz
> I'd like to turn this into...
> 3261 1317 Non-client 2 mmarkowitz
I'm not sure if this will help, but you may want to take a look at the
GROUP BY WITH ROLLUP and WITH CUBE commands. It may work for what you are
after.
SELECT Col1, Max(Col2)
FROM tablename
GROUP BY Col1
WITH ROLLUP
HTH
Rick Sawtell
MCT, MCSD, MCDBA|||SELECT col1, MAX(col2), MAX(col3), MAX(col4), ...
FROM YourTable
GROUP BY col1
--
David Portas
SQL Server MVP
--|||I don't think this will work, he wants to flatten one ofthe two columns in
his table out into multiple columns...
--
http://www.aspfaq.com/
(Reverse address to reply.)
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1103306604.369012.58920@.z14g2000cwz.googlegroups.com...
> SELECT col1, MAX(col2), MAX(col3), MAX(col4), ...
> FROM YourTable
> GROUP BY col1
> --
> David Portas
> SQL Server MVP
> --
>|||OK. Looks to me like 5 columns but I guess that's just presentational ;-)
Tough to pivot without an explicit attribute for the column.
--
David Portas
SQL Server MVP
--|||"Aaron [SQL Server MVP]" wrote:
> This looks like a PIVOT.
Actually it is the RESULT of a pivot, which is why it is spread out
vertically like that.
But I did figure out a "trick". After reading the page you sent, I combined
their technique of ISNULL (instead of CASE) with MIN, and presto.
Thanks!
(anyone interested in the code?)

Saturday, February 25, 2012

"Max row" in each group

I'm having trouble developing a query that will group on a given set of
columns and, for each group, will also display the other columns in the
"max row" for that group. Let me illustrate what I mean:
CREATE TABLE [dbo].[GroupTest] (
[testID] [int] IDENTITY (1, 1) NOT NULL ,
[office] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[startDate] [datetime] NOT NULL ,
[status] [int] NOT NULL ,
[endDate] [datetime] NULL ,
[reportDate] [datetime] NOT NULL ,
[amount] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[GroupTest] ADD
CONSTRAINT [PK_GroupTest] PRIMARY KEY CLUSTERED
(
[testID]
) ON [PRIMARY]
GO
Each row in this table represents a status update in an issue tracking
system. (The actual table has more columns. This is a subset for
illustrative purposes.) None of the columns is unique other than testID.
I am being asked to produce a query for each combination of {office,
startDate} will include the row matching that combination that has the
greatest value in the status column. So if office='ABC' and
startDate='2005-11-30' and there are two rows matching these values, one
with status=3 and one with status=4, the query should include the row
having status=4.
If the combination {office, startDate, status} were unique in this
table, the following query would do the job:
SELECT
G0.office, G0.startDate, G0.status,
G0.endDate, G0.reportDate, G0.amount
FROM GroupTest G0
INNER JOIN
(SELECT office, startDate, max(status) maxStatus
FROM GroupTest GROUP BY office, startDate) G1
ON G0.office = G1.office
AND G0.startDate = G1.startDate
AND G0.status = G1.maxStatus
order by G0.office, G0.startDate
But that's not the case. I asked the customer what to do. For their
purposes, the report will be fine if it can show them an "illustrative"
row--in other words, if more than one row in the table matches a given
combination {office, startDate, status}, the query should return one of
them arbitrarily. But I'm not figuring out how to do that. I guess I'd
like to figure out how to retrieve the "maximum row" for each {office,
startDate} combination, or something similar. Can anyone help?
I could punt and go with just displaying the maximum over each of the
columns on the right-hand side (status, endDate, reportDate, amount),
each taken independently:
SELECT
G0.office, G0.startDate, G0.status,
G0.endDate, G0.reportDate, G0.amount
FROM GroupTest G0
INNER JOIN
(SELECT office, startDate, max(status) maxStatus,
max(endDate) maxEndDate,
max(reportDate) maxReportDate,
max(amount) maxAmount
FROM GroupTest GROUP BY office, startDate) G1
ON G0.office = G1.office
AND G0.startDate = G1.startDate
AND G0.status = G1.maxStatus
AND G0.endDate = G1.maxEndDate
AND G0.reportDate = G1.maxReportDate
AND G0.amount = G1.maxAmount
order by G0.office, G0.startDate
But the rows produced by that query aren't "coherent": For a given
{office, startDate} combination, the status could be from one row, the
startDate could be from another, the endDate from another, etc. Each row
in my query should match an actual row in the table.I've moved on from my previous question, having figured out an approach.
However, I'm stuck on that approach because I'm getting unexpected
results. I'm asking about that in a new thread.

Friday, February 24, 2012

"Length" in table design view

I haven't been able to find an answer to this simple question from any of the
online documentation. In table design view, each one of my columns has a
datatype and length. Specifically, I have a column of dataype "ntext" with a
length of 16, but I don't know what that "16" means. 16 Bytes? How can I
determine the appropriate length to set it to, based on how many characters I
expect the field to hold?
Is there anywhere that explains in simple terms what each datatype is and
what the "length" means for each individual dataype? Thanks!
When text and image datatypes are stored in SQL Server, data page stores 16
byte pointer is stored that points to pages that stores the text\image data.

> How can I
> determine the appropriate length to set it to, based on how many
characters I
> expect the field to hold?
ntext datatype can store upto 1073741823 characters.
See more help on this in books online.
Vishal Parkar
vgparkar@.yahoo.co.in | vgparkar@.hotmail.com

"Know Issue" not in KB. COLID continues to rise.

I use transactional replication on several very "wide" table. One has
254 columns. After finding that was my limit for replication to work I
removed an unused columns and added a new needed column. Since the
COLID in the SYSCOLUMNS did not reset replication failed. I'm down to
renaming columns now.
This was in SQL Server 2000 and is still present in 2005. A call to
Microsoft turned up "Known Issue" statement.
Has anyone found another way around this without creating a new table
with the needed columns and moving the current data in? Keep in mind I
inherited this database and did not create a table this wide.
Thanks.
Mike.Mike,
apart from your suggestion, the only other workaround I am aware of is to
partition the table and to replicate the 2 tables separately, which is
probably more work than your method.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com

"Know Issue" not in KB. COLID continues to rise.

I use transactional replication on several very "wide" table. One has
254 columns. After finding that was my limit for replication to work I
removed an unused columns and added a new needed column. Since the
COLID in the SYSCOLUMNS did not reset replication failed. I'm down to
renaming columns now.
This was in SQL Server 2000 and is still present in 2005. A call to
Microsoft turned up "Known Issue" statement.
Has anyone found another way around this without creating a new table
with the needed columns and moving the current data in? Keep in mind I
inherited this database and did not create a table this wide.
Thanks.
Mike.Mike,
apart from your suggestion, the only other workaround I am aware of is to
partition the table and to replicate the 2 tables separately, which is
probably more work than your method.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com

"Key Column Information...."

Hi everybody!
I have the following problem: I have many tables in my database and for
some columns it happens that when I try to update them, I get the error
message, that the update doesn't work properly, because of "Key column
information insufficient or incorrect". This happens only for a few
columns in the table.
Example:
Table "tabProductColorPackage"
ID (INT, AutoSeed, Pkey)
Number (DECIMAL(18,2))
Unit (DECIMAL(18,2))
My VB-Code:
Recordset.Open "SELECT Unit FROM tabProductColorPackage WHERE ID=2"
Recordset.Fields("Unit") = 1
Recordset.Update
--> Works fine!
Recordset.Open "SELECT Number FROM tabProductColorPackage WHERE ID=2"
Recordset.Fields("Unit") = 1
Recordset.Update
--> Produces the error given in my description.
What the hell is going wrong here?
Thanks in advance,
BerndHi!
Sorry, the VB-Code that produces the error is as follows:
> Recordset.Open "SELECT Number FROM tabProductColorPackage WHERE ID=2"
> Recordset.Fields("Number") = 1
> Recordset.Update
Thanks,
Bernd

"Key Column Information...."

Hi everybody!
I have the following problem: I have many tables in my database and for
some columns it happens that when I try to update them, I get the error
message, that the update doesn't work properly, because of "Key column
information insufficient or incorrect". This happens only for a few
columns in the table.
Example:
Table "tabProductColorPackage"
ID (INT, AutoSeed, Pkey)
Number (DECIMAL(18,2))
Unit (DECIMAL(18,2))
My VB-Code:
Recordset.Open "SELECT Unit FROM tabProductColorPackage WHERE ID=2"
Recordset.Fields("Unit") = 1
Recordset.Update
--> Works fine!
Recordset.Open "SELECT Number FROM tabProductColorPackage WHERE ID=2"
Recordset.Fields("Unit") = 1
Recordset.Update
--> Produces the error given in my description.
What the hell is going wrong here?
Thanks in advance,
BerndHi!
Sorry, the VB-Code that produces the error is as follows:

> Recordset.Open "SELECT Number FROM tabProductColorPackage WHERE ID=2"
> Recordset.Fields("Number") = 1
> Recordset.Update
Thanks,
Bernd

Thursday, February 16, 2012

"Find In" Comma Separated Value

Hi,

****SQL Server related question.

I have a table in which one of the columns (col1) holds a string, like: 1,2,3,4,5,6,7,8,9,10

I am passing an int value (@.intValue) to the sproc.

What I want to be able to do is query the table like...


SELECT * FROM myTable where @.intValue ... is in col1

Any ideas?

Thanks a lot!!!!you can do something like

SELECT * FROM myTable WHERE col1 LIKE '%' + convert(varchar,@.intValue) + '%'

hth|||Thanks for the response ndinakar!

I tried your suggestion...

It does work...to a point. However... in my concatenated string, it is a possibility that I will have something that looks like this.

3,4,5,34,6,21

Then, when I query this table...say I am looking for '2' ...it returns the record with 21 in it.

Any other ideas about how I can go about this? I am thinking that I might have to break this concatenated value into separate columns.

Thanks for the reply. I greatly appreciate it!|||same approach, but you need to insure you have leading and trailing seperators

from this:
3,4,5,34,6,21

to this:
,3,4,5,34,6,21,

now you search for %,val,%

or in your example ,2, which wont match ,21,|||Thanks a lot! That works great...exactly what I was looking for.

Thursday, February 9, 2012

"Blank" Data Values in SQL Profiler

I'm built a trace in SQL Profiler and I have selected columns "DatabaseName" and "Filename" (among others) to be displayed. When I run the trace, the columns are displayed but the values are blank. The SQLServer 2K version is 8.0.760. Is this a bug or am I missing something?Not all profiler data columns are populated for all events that can be captured.

AFAICT "database name" will be populated for some events in the "security audit" catgory of events.

In the "trace properties" box , on the "data columns" tab, press the help button.

This should open up the BOL entry for "properties (data columns tab)"

click on the "monitoring with SQL Profiler Event categories" link at the bottom of that help page, and you get to see which columns may be relevant to each trace event.

Bill|||Thanks; the BOL tip is very helpful.

Friday, January 27, 2012

encrypt database

Hi,
Sorry for the message in spanish I confused the group.
Is there a way to encrypt a database (tables, columns, etc.) ? I want that
nobody can see the data model of my application
Probably it's impossible to deny access to the database, but at least if
someone is trying to copy my design he/she will see the name of objects,
columns, and others encrypted. (For example instead of see the table
ARTICLE, see symbols ="!!$%&/- )
I'll appreciate your comments.
Edmundo J. Davila
You can assign certificates and encryption keys under the security
folder of a database in SQL Server Management Studio, however, these
only encrypt the data stream that is sent from a SQL Server instance
to any SQL Server Agents.
If you install SQL Server to run from inside of Microsoft Virtual
Server, then the entire database file will be stacker compressed which
would be unreadable to human eyes. But, with any kind of encryption,
SQL Server will have to spend time decrypting fields as it walks any
kind of search and this will cause a significant performance hit.

encrypt database

Hi,
Sorry for the message in spanish I confused the group.
Is there a way to encrypt a database (tables, columns, etc.) ? I want that
nobody can see the data model of my application
Probably it's impossible to deny access to the database, but at least if
someone is trying to copy my design he/she will see the name of objects,
columns, and others encrypted. (For example instead of see the table
ARTICLE, see symbols ="!!°$%&/- )
I'll appreciate your comments.
Edmundo J. DavilaYou can assign certificates and encryption keys under the security
folder of a database in SQL Server Management Studio, however, these
only encrypt the data stream that is sent from a SQL Server instance
to any SQL Server Agents.
If you install SQL Server to run from inside of Microsoft Virtual
Server, then the entire database file will be stacker compressed which
would be unreadable to human eyes. But, with any kind of encryption,
SQL Server will have to spend time decrypting fields as it walks any
kind of search and this will cause a significant performance hit.