Can anyone explain how this constraint functions and how its affects insertion of records into this table? Running win sql 2k5. Having issue with trying to insert data.
ALTER TABLE [dbo].[ws_shiptable] ADD CONSTRAINT [DF_ws_shiptable_ps_processed] DEFAULT (0) FOR [ps_processed]
It adds a default value of 0 for column ps_processed. If you do an insert and you don't specify a value for ps_processed, it'll default to 0. What is the issue you are having? Do you get error messages you can post? A default value shouldn't give you trouble, but make sure ps_processed has the right data type. I suspect it'll be a bit field?
If no value is provided for the column ps_processed then SQL Server will set the value to zero
Related
In SSMS, trying to add some test data by editing top 200 rows. How do I get a uuid to paste into the cell? I'm trying not to have to write insert statements.
Have tried getting uuids from an online uuid generator - tried all formats from this generator. Reviewing other stack overflow questions seem to relate to programming rather than editing issues with uuids. Tried adding uuid enclosed with single quotes and without single quotes.
unfortunately the field is defined as binary 16, not null.
AFAIK, Enterprise Manager doesn't allow pasting into binary fields.
If you define the column with the correct data type of uniqueidentifier your paste should work just fine.
https://learn.microsoft.com/en-us/sql/t-sql/data-types/uniqueidentifier-transact-sql?view=sql-server-2017
The fix isn't to use an INSERT statement (although that is a workaround for right now), the fix is to use the correct datatype.
As an added benefit, the uniqueidentifier column won't allow invalid values, while binary will handle any bytes you can jam in there.
The error message says:
You cannot use the results pane to set this field to values other than null
I would presume this means you cannot supply a guid in the grid, you have to leave it null
Does the column have a default value of NewID()? If so, it should fill in itself when you commit the row (focus another row)
If not, try inserting data by writing an INSERT statement:
INSERT INTO table(list,of,columns,except,auto,generated,ones)
VALUES ('list','of','values'...)
Or by selecting/modifying them from elsewhere:
INSERT INTO table(list,of,columns,except,auto,generated,ones)
SELECT mixed,'list','of','values',and,columns
FROM othertable
A colleague created a table with a strange UNIQUE key (to get around NULL handling) by adding a COALESCE to enforce NULL values in the unique constraint. I'm receiving the error below when trying to open the database using DbVisualizer Pro, but it seems to open fine when using SQLite Expert Professional (I'm trialing them both):
SQLITE_CORRUPT: The database disk image is malformed (malformed database schema (ux_test) - near "(": syntax error)
DDL:
CREATE TABLE Test (
Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
val1 integer,
val2 integer,
val3 integer
);
CREATE UNIQUE INDEX ux_test ON Test(val1, COALESCE(val2, -1), COALESCE(val3, -1));
I asked DbVisualizer support for help regarding this, and was told that it's a SQLite error. Can anyone offer any insight into this? FYI, the COALESCE() is in the Unique key because of the way SQLite handles NULL values; it doesn't see NULL values being equal in the constraint. Additionally, they prefer to use INSERT OR REPLACE INTO if the constraint is hit, so a BEFORE trigger won't work.
Thanks.
EDIT: CL's answer (below) seems to the issue; DbVisualizer Pro is using SQLite version 3.8.11, vs the current version of 3.18.0. I'm trying to see if I can get an update to the dll's used because it's really a great application. Thank you!
The documentation says:
The ability to index expressions was added to SQLite with version 3.9.0 (2015-10-14). A database that uses an index on expressions will not be usable by earlier versions of SQLite.
Obviously, your DbVisualizer is horribly outdated.
I'd like to see the ability to attach custom error messages to CONSTRAINT objects, specifically CHECK constrints. Either directly or via a custom error number in sysmessages.
I've seen developers have to create triggers. I think that's not a good reason to implementing it.
I'm using SQL SERVER 2008.
You could name your constraint with a user message.
For Example:
ADD CONSTRAINT
[Foo cannot be greater than Bar. Please be sure to check your foos and bars next time.]
CHECK (foo <= Bar)
I know this is an old post, but I've found something that may make it a bit easier to provide clearer error messages for check constraints to the end-user: the names of check constraints can include carriage returns and line feeds, so the error message can be made a bit easier to see.
E.g. creating the following constraint produces the error message below. (the blank lines between the [ and ] are intentional i.e. they are part of the constraint name.)
ALTER TABLE dbo.Sales WITH CHECK ADD CONSTRAINT [
ERROR:
You have stupidly entered a negative selling price. Please report to detention.
] CHECK ([SellingPrice] >= 0.00)
GO
And when this constraint fails, the resulting message is:
I tried putting markup in the error message (i.e. constraint name), like <b>message</b> and *message*, but to no avail. And it may be possible, but really unwieldy, to use this for foreign key constraints as well. I haven't tried it.
So it's not a 100% solution, but hopefully easier for the user to see the intended error message.
Edit (2022-02-09): Since database object names are stored using the sysname data type (search for 'sysname' on this page), they cannot be longer than 128 characters. Use short error messages 😄
You can't directly
A CHECK constraint fails with a standard error message. You could use a TRY/CATCH block to parse the error and throw your own (RAISERROR) or use a trigger.
I'd check first so it doesn't fire, either is SQL or in client code. And of course you leave the constraint there to protect data integrity
So if you have a constraint
ALTER TABLE MyTable WITH CHECK
ADD CONSTRAINT CK_MyTable_foobar CHECK (#foo <= #Bar)
You run the following SQL code or equivalent in your client code:
...
IF #foo > #bar
RAISERROR ('foo (%i) can not be greater than bar (%i)', 16, 1, #foo, #bar)
INSERT MyTable (foo, bar) VALUES (#foo, #bar)
....
Is it possible to force a field to always be a certain value?
We have a website in production that writes a value entered by the user into a string field. Now, our requirements have changed and we no longer actually want to save this value. For technical reasons, we don't want to do a new publish just for this if we don't have to.
What would be ideal is to ALTER the table in such a way that the field will always be NULL and that existing INSERTS and UPDATES into the field will work as normal but SQL Server will NULL the field regardless.
This is a temporary thing. We will eventually change the code to not write this value.
Just looking for a quick way to NULL the field without changing code, republishing etc...
Is this possible? Is writing a trigger the only solution?
Thanks!
You could make an INSERT and UPDATE after trigger that will NULL the field each time it is updated / inserted into.
This is the quickest and easiest solution.
I am in the process of converting an Access database to SQL Server 2005. I have successfully migrated the data and original schema using SSMA and am now in the process of normalizing the database, which requires me to add a few unique identifiers.
Some of the columns we had were previously created using an AutoNumber data type, which is fine. However, I need to create meaningless but unique identifiers for other data, so I am using the int data type with the Identity Specification property. I am seeding at '101' to keep this data above the range that currently exists for data that already has unique identifiers, as they will eventually reside in the same table.
My problem is that when I create a new int with Identity Specification with a seed value of '101' and an increment of '1', the numbers start at '1'. I have attempted to reseed with:
USE dbMyDatabase
DBCC checkident(tblMyTable, reseed, 101)
to no avail. Any suggestions would be greatly appreciated. Thanks in advance!
The solution was to create the column by using a SQL query manually. Adding it through the "New Column..." option produced incorrect results every time. Now that I added it with
USE dbMyDatabase
ALTER TABLE tblMyTable
ADD fldID INT IDENTITY(101,1)
it works just fine.