ALTER TABLE using WHERE clause condition - sql

I was under the assumption that they was a WHERE Clause for ALTER and I have understood now after some research that WHERE Clause doesn't exist for ALTER Command. How to handle cases where we might need to check some conditions in the ALTER Command?
this is my query , what's im doing wrong please ?
ALTER TABLE cp_asset_translations CHANGE caption caption VARCHAR(1000) DEFAULT NULL WHERE LENGTH(caption) > 255;

There is no WHERE. And there is no ability to change the length of a column in some rows but not others. But that is not a problem. You can change the length of the caption to VARCHAR(1000) and because the string is variable length, no additional space is used for shorter strings:
ALTER TABLE cp_asset_translations CHANGE caption caption VARCHAR(1000) DEFAULT NULL;
Note: I assume that this ALTER TABLE is valid in the database you are using. The statement varies significantly across databases.

Related

False error message for binary data truncation(Msg 8152)

I am getting false error message while data inserting to some table.
The data type for the field is Varchar(20) and the data being inserted has max
data length is 6.
I don't understand where's the issue is. Although I can avoid this by adding
SET ANSI_WARNINGS OFF
But it'd be workaround not a solution:
It seems your column SchemaName maxlength property smaller then value which you going to insert.
Update your column length as per your data.
ALTER TABLE Temp ALTER COLUMN SchemaName VARCHAR(XXXX)
I just found the root cause of this issue.
Actually I was trying to insert sysname data type value into varchar().
So must specify proper length to hold sysname data type.
Your target table is not sufficient to hold the data that you are trying to insert. It seems your target size would be varchar(256) but you have given varchar(10). Just extend the column size to resolve this issue.
Run this DDL
Alter table temp alter column SchemaName varchar(256)
This error come when your column has less size them.
Update size of your column.

ERROR: syntax error at or near "modify" - in postgres

I executed this SQL statement in Postgres
alter table user modify column 'distinguishedName1' text;
and
alter table user modify column distinguishedName1 text;
user is the table name
distinguishedName1 is the column name with integer data type.
I wanted to modify the data type to boolean or text or varchar(256) etc based on user's input. But when I run the query I get the error
ERROR: syntax error at or near "modify"
Not sure what is the problem. Help required on right query.
POSTGRES syntax for altering column type :
ALTER TABLE user ALTER COLUMN distinguishedName1 TYPE text;
Try this:
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text USING code::text;
or
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text
Also do note that the USING is optional. See the manual here:
The optional USING clause specifies how to compute the new column
value from the old; if omitted, the default conversion is the same as
an assignment cast from old data type to new. A USING clause must be
provided if there is no implicit or assignment cast from old to new
type.
On a side note try to avoid naming your tables as reserved keywords.
alter table user Alter column distinguishedName1 text;
Syntax mistake , for sql server you have to use alter to modify the column of table

Remove CHARACTER SET UNICODE_FSS from a column in a firebird database

I have a Firebird database with several tables in it. There are several columns who were added when database was created as
alter table Machines add MachineVersion varchar(100) CHARACTER SET UNICODE_FSS
I want to modify these columns to drop the CHARACTER SET UNICODE_FSS so I ran the command
alter table Machines alter column MachineVersion type VARCHAR(100)
Still, when I open the database in SQL Manager the character set for these columns is still UNICODE_FSS.
Is there another syntax for the second command to remove the CHARACTER SET UNICODE_FSS?
alter table Machines alter column MachineVersion type VARCHAR(100)
This query won't change the character set.
If you want to remove charater set you should alter domain like:
update RDB$FIELDS set
RDB$CHARACTER_SET_ID = NULL
where RDB$FIELD_NAME = 'RDB$141'
Instead of RDB$141 use column domain
It is possible to add new column , copy data from old column to new and later to drop old column?

Creating a sequence on an existing table

How can I create a sequence on a table so that it goes from 0 -> Max value?
I've tried using the following SQL code, but it does not insert any values into the table that I am using:
CREATE SEQUENCE rid_seq;
ALTER TABLE test ADD COLUMN rid INTEGER;
ALTER TABLE test ALTER COLUMN rid SET DEFAULT nextval('rid_seq');
The table I am trying to insert the sequence in is the output from another query. I can't figure out if it makes more sense to add the sequence during this initial query, or to add the sequence to the table after the query is performed.
Set the default value when you add the new column:
create sequence rid_seq;
alter table test add column rid integer default nextval('rid_seq');
Altering the default value for existing columns does not change existing data because the database has no way of knowing which values should be changed; there is no "this column has the default value" flag on column values, there's just the default value (originally NULL since you didn't specify anything else) and the current value (also NULL) but way to tell the difference between "NULL because it is the default" and "NULL because it was explicitly set to NULL". So, when you do it in two steps:
Add column.
Change default value.
PostgreSQL won't apply the default value to the column you just added. However, if you add the column and supply the default value at the same time then PostgreSQL does know which rows have the default value (all of them) so it can supply values as the column is added.
By the way, you probably want a NOT NULL on that column too:
create sequence rid_seq;
alter table test add column rid integer not null default nextval('rid_seq');
And, as a_horse_with_no_name notes, if you only intend to use rid_seq for your test.rid column then you might want to set its owner column to test.rid so that the sequence will be dropped if the column is removed:
alter sequence rid_seq owned by test.rid;
In PostgreSQL:
UPDATE your_table SET your_column = nextval('your_sequence')
WHERE your_column IS NULL;
I'm not fluent in postgresql so I'm not familiar with the "CREATE SEQUENCE" statement. I would think, though, that you're adding the column definition correctly. However, adding the column doesn't automatically insert data for existing rows. A DEFAULT constraint is for new rows. Try adding something like this afterwards to populate data on the existing rows.
DECLARE #i Int
SET #i = 0
SET ROWCOUNT 1
WHILE EXISTS (SELECT 1 FROM test WHERE rid IS NULL) BEGIN
UPDATE test SET rid = #i WHERE rid IS NULL
END
SET ROWCOUNT 0

SQL Server 2008 stored procedure result as column default value

First of all, thank you guys. You always know how to direct me when I can't even find the words to explain what the heck I'm trying to do.
The default values of the columns on a couple of my tables need to be equal the result of some complicated calculations on other columns in other tables. My first thought is to simply have the column default value equal the result of a stored procedure. I would also have one or more of the parameters pulled from the columns in the calling table.
I don't know the syntax of how to do it though, and any time the word "stored" and "procedure" land next to each other in google I'm flooded with info on Parameter default values and nothing relating to what I actually want.
Half of that was more of a vent than a question...any ideas though? And plz plz don't say "Well, you could use an On-Insert Trigger to..."
You can't have the default be the result of a stored procedure, it has to be a function. If you can convert the procedure into a function, then you can use that function. If you cannot, then you must use a trigger.
You would have to convert that stored procedure to a user-defined function. There are different types of UDF's - the one you're looking at would be the scalar UDF - returning a single value.
So for instance you could create a function like this:
CREATE FUNCTION dbo.YourDefaultFunction(#Input1 INT, #Input2 VARCHAR(10))
RETURNS VARCHAR(100)
AS BEGIN
DECLARE #Result VARCHAR(100)
SET #Result = #Input2 + ' - giving: ' + CAST(#Input1 AS VARCHAR(5))
RETURN #Result
END
Of course, yours would be a lot more complicated :-)
Once you've done that, you can define this to be the default for a column of type VARCHAR(100) - either directly when declaring the table, or later on via an ALTER TABLE statement:
CREATE TABLE dbo.YourTable(.......
SomeColumn VARCHAR(100)
CONSTRAINT DF_YourTable_SomeColumn
DEFAULT (dbo.YourDefaultFunction(417, 'Test')),
.....)
or :
ALTER TABLE dbo.YourTable
ADD CONSTRAINT DF_YourTable_SomeColumn
DEFAULT (dbo.YourDefaultFunction(4711, 'Test2'))
FOR SomeColumn
Unfortunately, you cannot pass other columns as parameters to your function when defining it as a default value for a column.
Does that help??