Showing posts with label key. Show all posts
Showing posts with label key. Show all posts

Tuesday, March 27, 2012

Are Blank String and String with only Space characters equal to each other? They should not be!!

I have a table called Table_1

Table_1
--
a varchar(10),
b varchar(20)

and Column a is the Primary Key.

INSERT INTO Table_1
(a ,b)
VALUES
('' , 'BLANK STRING')

INSERT INTO Table_1
(a ,b)
VALUES
(' ' ,'4 SPACES')

When i execute this Insertions, it inserts the First Record but on the Second one it gives Violation of Uniqueness

(1 row(s) affected)
Msg 2627, Level 14, State 1, Line 9
Violation of PRIMARY KEY constraint 'PK_Table_1'. Cannot insert duplicate key in object 'dbo.Table_1'.
The statement has been terminated.

My Questions is that; HOW can a Blank String or a String with 4(or any number) space charcters in it can be EQUAL? In my opinion they should not be regarded as the same! Is there a solution to this issue? That's not the case in Oracle Database.

Thanks for your help!

Regards,
Cem

According to ANSI standards (or is it SQL Server's implementation of ANSI...), anyway, for sorting purposes, leading blanks are ignored. (And Primary Keys are 'sorted'...)

Try these two statements, the first will provide the 'right' answer, and the second will provide the 'real' answer.


SELECT len( (replicate( ' ', 4 )) )
SELECT datalength( (replicate( ' ', 4 )) )

|||

You should check the setting of SET ANSI_PADDING. If you want behavior such as you find in ORACLE, you will need SET ANSI_PADDING ON. That way trailing blanks will not be trimmed from your VARCHAR insert statements.

http://msdn2.microsoft.com/en-us/library/ms187403.aspx

Dan

Tuesday, March 20, 2012

Approach Help to load Data from Flatfiles into relational table where Data is coming as spaces i

Hi ,

My Input is a flat file source and it has spaces in few columns in the data . These columns are linked to another table as a foreign key and when i try loading them in a relational structure Foreigh key violation is occuring , is there a standard method to replace these spaces .

what approach should i take so that data gets loaded in a relational structure.

for example

Name Age Salary Address

dsds 23 fghghgh

Salary description level

2345 nnncncn 4

here salary is used in this example , the datatype is char in real scenario

what approach should i take to load the data in with cleansing the spaces in ssis

Assumption: You don't have any control over the structure of the destination tables, but you can add records.

To maintain your foreign key constraint, you will need to create a "salary" record that you can use where salary is not defined. EG:

Salary Description Level

9999 Not Assigned 1

Then you can use a Derived Column expression to create a new column in your dataset that uses an If expression and then write that derived column to your destination table

Like this:

Code Snippet

[Salary]=="" ? 9999 : [Salary]

This is not an optimal solution (even though it is very practical.) Ideally you should restructure your tables so:

Assumption: You can change the structure of your destination tables

In this case, you need to take the Salary column out of your destination table, and put it into another table. The relationship is that employees can have zero or one salary code. So you can create a table relating employees to salary codes with two columns (EmployeeNumber, SalaryCode) and a primary key on EmployeeCode.

(This is a drastic simplification, of course. In a real-world scenario you would probably want to track historical information too.)

|||

Thanks for the Quick reply i tried doing as you said using derived transformation and using replace function but it doesnot seem to capture the space as value in the replace expression in ssis.

Replace(salary," ",9999) it does not work it says that there might be a possibility of divide by zero error. what expression should i use here to replace the values

|||

first of all, since this is a char value, you should put quotes around the 9999.

Seond, I didn't say to use the REPLACE function, though I guess that would work, but you run the risk of having a valid id messed up by replacing a leading or trailing space with a 9999.

Also, if your Salary code was " " (eight spaces) then the REPLACE function will put in:

"99999999999999999999999999999999" which migh be too big for your destination field.

So, try using TRIM first to get ris of spaces, then ?: to replace blank with 9999.

Put this into the Expression column of Derived Value step:

Code Snippet

TRIM([SALARY])=="" ? "9999" : TRIM([Salary])

sql

Sunday, March 11, 2012

Apply insert duplicated key record

Hi~
When insert, just insert if it is a new item or update it if duplicated. The update operation should be summing or averaging. Anyone help?

INSTEAD OF INSERT Trigger
-Check for dupe
--If dupe, increment
--Else INSERT

Adamus

|||

Thank you for reply.

But would you give me some sample query?

|||

1. Validate if the data availabile in the Table

2. If Yes , execute the Update command

3. If No, execute the Insert command

(ex)

if Exists (Select * From SomeTable Where SomeColumn=SomeValue)

Update

Set

SomeValueColumn = SomeValueColumn + New Value

Where

SomeColumn=SomeValue

Else

Insert Into SomeTable values (...)

|||

Thank you for your help.
Here is more specific case.

CREATE TABLE mytable
(
id1 int,
id2 int,
value1 int,
PRIMARY KEY(id1,id2)
);

CREATE TRIGGER mytrigger ON mytable
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS(SELECT * FROM INSERTED I,mytable M
WHERE I.id1=M.id1 AND I.id2=M.id2)
UPDATE mytable SET value1 = value1 + 100
ELSE
INSERT INTO mytable SELECT id1,id2,value1 FROM INSERTED
END

BULK INSERT mytable FROM 'mydatafile_1.dat' WITH (FIRE_TRIGGERS);(1)
BULK INSERT mytable FROM 'mydatafile_2.dat' WITH (FIRE_TRIGGERS);(2)
BULK INSERT mytable FROM 'mydatafile_3.dat' WITH (FIRE_TRIGGERS);(3)

I want insert a record if new item, update it by adding new item value to the original value.

Suppose the 'mydatafile_1.dat' has all new items(1).
'mydatafile_2.dat' has some new items and some duplicated items(2)
and 'mydatafile_3.dat' has all new items(3).

Only (1) and (3) are applied correctly. (2) is updated all of record but never inserted the new items.
It seems that the validation part should be repaired.
And, I want to change 100 to the new inserted value.

Saturday, February 25, 2012

application of fulltext search

Hello, It is possible to search text in my blog use a query sentence instead
of several key words? Which I mean, if I want to get to know "How rich is
Bill Gates?", I can simply enter this sentence instead of enter
"Bill+Gates+rich+how" in my search textbox. If it is applicable, how?I want
to rewrite the code of my blog
Thanks, indeed
Search is unable to interpret the context of your query. English Query has
rich context sensing abilities but it works against a database.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"SuperRock" <moyuji@.gmail.com> wrote in message
news:eTU999zbHHA.2188@.TK2MSFTNGP04.phx.gbl...
> Hello, It is possible to search text in my blog use a query sentence
> instead of several key words? Which I mean, if I want to get to know "How
> rich is Bill Gates?", I can simply enter this sentence instead of enter
> "Bill+Gates+rich+how" in my search textbox. If it is applicable, how?I
> want to rewrite the code of my blog
> Thanks, indeed
>
|||Does your blogging solution use SQL Server as the data store? Please
elaborate (give comprehensive examples, post DDL and sample data...).
ML
http://milambda.blogspot.com/

Thursday, February 16, 2012

Append query from Access Table to Linked SQL Server Table Failing

Strange one here - I am posting this in both SQL Server and Access forums

Access is telling me it can't append any of the records due to a key violation.

The query:

INSERT INTO dbo_Colors ( NameColorID, Application, Red, Green, Blue )
SELECT Colors_Access.NameColorID, Colors_Access.Application, Colors_Access.Red, Colors_Access.Green, Colors_Access.Blue
FROM Colors_Access;

Colors_Access is linked from another MDB and dbo_Colors is linked from SQL Server 2000.

There are no indexes or foreign contraints on the SQL table. I have no relationships on the dbo_ table in my MDB. The query works if I append to another Access table. The datatypes all match between the two tables though the dbo_ tables has two additional fields not refrenced in the query.

I can manually append the records using cut and paste with no problems.

I have tried re-linking the tables.

Any ideas?
Thanks,
BradI'd guess that the SQL Server db you think you are linking to in dbo_Colors isn't really the one you think. Perhaps the login/password in your datasource is connecting to a different database than the one you expect?

To check, get the name of the constraint being violated and check in the sql server table to see if that constraint exists

Also, try running the sql server profiler to see what sql server db ms access is trying to insert data into|||mattrevs,

It does appear that I am linking the right table. There is only one Colors table and only one database that has a colors table.

Could you tell me how to check which constraint is being violated? After I manually pasted the data into dbo_Colors, I ran checkconstraints() and no error were reported.

I also tried running the query with implicit_transaction OFF and still no joy.

I ran the trace and here are the last two lines. The first one was simply duplicated for each record:

RPC:Completed exec sp_executesql N'INSERT INTO "dbo"."Colors" ("NameColorID","Application","Red","Green","Blue") VALUES (@.P1,@.P2,@.P3,@.P4,@.P5)', N'@.P1 nvarchar(38),@.P2 nvarchar(30),@.P3 int,@.P4 int,@.P5 int', N'{FFC28EAD-1134-40BB-9723-7D88A0B0AC7A}', N'Tile1', 197, 183, 156 Microsoft Access sa 0 11 0 0 2564 54 2004-06-21 10:35:14.170

SQL:BatchCompleted IF @.@.TRANCOUNT > 0 ROLLBACK TRAN Microsoft Access sa 0 0 0 0 2564 54 2004-06-21 10:35:19.403

- Brad|||You say that pasting the data one row at a time from within ms access works ok?

If so, maybe you could also perform a sql trace on this and see what is different?|||I got it figured out. (BTW, I did the trace with the pasting but the results were ... confusing)

Apparently a bit field in SQL server can be Null?

There are two additional fields in the SQL version of the table. The NVarChar field I had set to allow nulls. I never bothered with the Bit field.

When I checked Allow Nulls on the bit field and re-linked, my query worked.

I guess my lesson her is never assume anything. I have learned that lesson many times and will probably learn it again in the (near) future.

Thanks for your help,
Brad

Sunday, February 12, 2012

Anyway to set a primary key value

Hi just wondering if anyone knows if there is anyway to set a primary key
value? for example
pri key vharchar
1 paul
2 tom
999 Not Active
Can I somehow set the primary key for Not Active to 999?
thanks.
Paul G
Software engineer.
If it is a one-time thing, take a look at IDENITY_INSERT
Rick Sawtell

Anyway to set a primary key value

Hi just wondering if anyone knows if there is anyway to set a primary key
value? for example
pri key vharchar
1 paul
2 tom
999 Not Active
Can I somehow set the primary key for Not Active to 999?
thanks.
--
Paul G
Software engineer.Figured out a way around this one.
"Paul" wrote:
> Hi just wondering if anyone knows if there is anyway to set a primary key
> value? for example
> pri key vharchar
> 1 paul
> 2 tom
> 999 Not Active
> Can I somehow set the primary key for Not Active to 999?
> thanks.
> --
> Paul G
> Software engineer.|||If it is a one-time thing, take a look at IDENITY_INSERT
Rick Sawtell

Anyway to set a primary key value

Hi just wondering if anyone knows if there is anyway to set a primary key
value? for example
pri key vharchar
1 paul
2 tom
999 Not Active
Can I somehow set the primary key for Not Active to 999?
thanks.
Paul G
Software engineer.If it is a one-time thing, take a look at IDENITY_INSERT
Rick Sawtell

Anyway to find informatoin on how a record is deleted?

Hi,
One record, linked by a key tid, is deleted from several tables in the
database. Is there anyway to find information on when and who deleted the
recrod from SQL server?
Thanks,
AlphaYou can use a log viewing tool, such as:
Apex SQL Log
http://www.apexsql.com/index.asp?PageId=apex_sql_log&TitleID=products
Lumigent Log Explorer
http://www.lumigent.com/products/le_sql.html
Log P.I.
http://www.logpi.com/
--
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Alpha" <Alpha@.discussions.microsoft.com> wrote in message
news:B6572BE4-B0FE-4034-976F-EA21D1C5CC8F@.microsoft.com...
> Hi,
> One record, linked by a key tid, is deleted from several tables in the
> database. Is there anyway to find information on when and who deleted the
> recrod from SQL server?
> Thanks,
> Alpha

Anyway to find informatoin on how a record is deleted?

Hi,
One record, linked by a key tid, is deleted from several tables in the
database. Is there anyway to find information on when and who deleted the
recrod from SQL server?
Thanks,
Alpha
You can use a log viewing tool, such as:
Apex SQL Log
http://www.apexsql.com/index.asp?Pag...tleID=products
Lumigent Log Explorer
http://www.lumigent.com/products/le_sql.html
Log P.I.
http://www.logpi.com/
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Alpha" <Alpha@.discussions.microsoft.com> wrote in message
news:B6572BE4-B0FE-4034-976F-EA21D1C5CC8F@.microsoft.com...
> Hi,
> One record, linked by a key tid, is deleted from several tables in the
> database. Is there anyway to find information on when and who deleted the
> recrod from SQL server?
> Thanks,
> Alpha

Anyway to find informatoin on how a record is deleted?

Hi,
One record, linked by a key tid, is deleted from several tables in the
database. Is there anyway to find information on when and who deleted the
recrod from SQL server?
Thanks,
AlphaYou can use a log viewing tool, such as:
Apex SQL Log
http://www.apexsql.com/index.asp?Pa...itleID=products
Lumigent Log Explorer
http://www.lumigent.com/products/le_sql.html
Log P.I.
http://www.logpi.com/
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Alpha" <Alpha@.discussions.microsoft.com> wrote in message
news:B6572BE4-B0FE-4034-976F-EA21D1C5CC8F@.microsoft.com...
> Hi,
> One record, linked by a key tid, is deleted from several tables in the
> database. Is there anyway to find information on when and who deleted the
> recrod from SQL server?
> Thanks,
> Alpha