Update a field only if it is empty in SQL Server 2005? - sql-server-2005

i have column which is not mandatory up to now, now it becomes a mandatory field and an unique field, now it contains empty. now i want update that field. i am in quite confusion to do that.see the below picture.update the segment_NO with out dupicates
the answer should be like this

You can write the update query in a loop which test for null condition.

I think you need to add some details to the question.
Still you can use simple INSERT query to insert in the table or either you can use Update query to update a record.
http://msdn.microsoft.com/en-us/library/8hwekas8%28v=vs.80%29.aspx
Refer this URL for further reference.

Kindly specify the issue bluntly and with out any confusions.. Well somewhat i can understand is... You should update your DB table and make a primary key of two columns in composite style..
Hopefully this will be answer for your question

Related

Interview: How to handle SQL NOT NULL constraint on the code end

I was recently asked this question in an interview an I'm having trouble formulating the question well enough to find an answer via search engine.
If my SQL database has a NOT NULL constraint placed on the "name" column, how would I be able to create that row, filling it with other data, without tripping the "name" NOT NULL constraint, assuming that you don't have the proper data to insert into the "name" field?
My off the cuff response was to insert an empty string into the "name" field, but I feel like that's too hacky. Does anyone know the proper response?
It's usually a best practice to insert a dummy value such as a -1 that you can easily replace later. A blank string can be more problematic in some cases. To do this you would either use a CASE WHEN statement, or ideally, an ISNULL() function which would look like this ISNULL([ColName], -1) ISNULL is probably the answer they were looking for. That would insert the data if you have it and then if it's null, it would insert a -1.
As Gordon commented, you could also use a DEFAULT value when creating the table. In my answer above, I am assuming you're working with a table that had already been created - meaning you couldn't do that without altering the table.
There are two ways that I can think of for not having to insert name if it is NULL. By far the simpler is to define a default value:
alter table t alter column name varchar(255) not null default '<no name>';
The alternative is to use a trigger, but that is much more cumbersome.
If I were asking a similar question, this is the answer that I would want.
Why bypass the constraint?
In my opinion either your data is wrong or the constraint.
If you bypass constraints, you can't assure some data quality inside the db.
So, i would say the scenario where a table could not be changed even if the constraint is wrong is a huge technical debt which should be solved instead.

Autofill a column according to another T-SQL statement

Sorry to disturb you again. lol :)
I am looking at the answers given on similar questions but they are still not very helpful.
Here's my case:
I want to change the schema on my table to auto fill one column.
What I understand is the update function is only for values, and would not apply when new values will be entered in the future.
I want to be able to set the Gname according to the Grade so that I can have. so that I can have 1 for novice or new, 2 for standard, 3- intermediate and 4- Expert.
Can someone help please? or direct me towards the right documentation.
Thanks to you all
typically this would be done through the use of a lookup table.
So you would have your table you have then add another table for Gname that would carry the Grade Id 1,2,3,4 and the Name. Then when you want to add that as a column to your query you would do that through joining your gname table with your table on the ID.
however since sql-server 2008 there is also a Computed Column https://msdn.microsoft.com/en-us/library/ms188300(v=sql.105).aspx
If you want to enact the computed column you could alter your table and add it.
ALTER TABLE TableName
ADD ComputedColumnName AS (CASE WHEN Grade = 1 THEN 'novice' WHEN Grade = 2 THEN 'standard'.....END)
Yet another method some programmers use is to not put the lookup anywhere in the database but rather use an enumeration in their application layer to handle the translation.
The first method is less demand of storage space in your database and doesn't require schema/table definition change to rename or add additional gnames. Plus it leads to more data integrity, because I have seen enumerations mess up a pretty significant eCommerce site and cause programmers a lot of headaches trying to debug.

Linking sql tables in table rows

I am unsure how to ask this, but I will give it my best shot. Currently, I have a few tables which hold indices into other tables to get information. For example, I have a row with name_id as 1, which indicates that I should look at the 'name' table and go to index 1. Is there anyway to directly link a row within a row? Is there another design choice which would be much better than this? Thanks!
You want to use what are called foreign keys, here is an explanation of them in Sqlite.
this is the preferred method of database design
you write a sql statement that uses a JOIN to get the linked rows back.

If exist update else insert records in SQL Server 2008 table

I have one staging table and want to insert data to Main table, so i want to check while inserting data from staging to Main table, if exists then update the records else insert as new records. Here the issue is both the staging as well as Main table does not have any key column based on which i can compare values.
Is it possible to do without having key columns i.e. primary key on both the tables? if yes, please, suggest me how.
Thanks in advance.
If there is no unique key or set of data within a row to define uniqueness, then no.
The set of data can be a combination of the data in each column, creating a sum of parts which will provide uniqueness; however without exposure to your data you would need to make that decision.
You write the WHERE-clause to include all the fields that make your record unique (ie. the fields that decide whether the record is new or should be updated.)
Take a look at this article (http://blogs.msdn.com/b/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx) for hints on how to construct it.
If you are using SQL Server 2008r2, you could also use the MERGE statement - I haven't tried it on tables without keys, so I don't know whether it would work for you.

SQL: Best way to perform Update record operation to any given MySQL table

im programming a app to perform CRUD to any given table in any given database (MySQL).
Im having trouble figuring the best way to deal with the Update operation.
I was thinking: 1)Find Primary Key on table & 2)Update record according to Primary Key field coincidence between both records (incoming and allready present in MySQL table).
I know that while Primary Key in every table is very suggested it is still optional, so as i said im not sure if theres a better aproach since my method would not work with a table without a Primary Key.
Thanks in advance.
The answer i found that i believe is valid is the following: For the Update action send two records to the server, the non updated one and the updated one.
The server side has to include each field of the non-updated record in the where clause of the update query with LIMIT=1 (to avoid problems with duplicated records).