What's wrong with my `ALTER TABLE` statement? - sql-server

I keep receiving the error:
Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword
'as'.
My code
ALTER TABLE [SupplierDetails]
ADD [practice] varchar(300) as CONCATENATE(SupplierDescription, SupplierEmail)

I don't know of any SQL Server function called CONCATENATE() although there is one called CONCAT(). In any case, you can just use the plus operator to achieve what you want:
ALTER TABLE [SupplierDetails]
ADD [practice] as (SupplierDescription + SupplierEmail)
As this SO answer mentions, you might be better off handling the concatenation in a SELECT or UPDATE statement.

First of all CONCATENATE(SupplierDescription, SupplierEmail) this is not a valid function in SQL server.
Try this
ALTER TABLE [SupplierDetails]
ADD [practice] varchar(300)
update SupplierDetails
set practice=SupplierDescription + SupplierEmail
or
ALTER TABLE [SupplierDetails]
ADD [practice] AS (SupplierDescription + SupplierEmail)

Try this.
ALTER TABLE [SupplierDetails]
ADD [practice] AS CONCAT(SupplierDescription,' ',SupplierEmail)
OR
ALTER TABLE [SupplierDetails]
ADD [practice] AS (SupplierDescription+SupplierEmail)
OR
ALTER TABLE [SupplierDetails]
ADD [practice] AS CONCAT(SupplierDescription,SupplierEmail)

I assume you want to create a computed column, which is fine.
The issue is that SQL Server doesn't have a function, called CONCATENATE, you either need to use a + sign, or use CONCAT(). In order to make your ALTER TABLE statement correct, you have to do this:
If your SQL Server version is 2008 R2 or older, which doesn't support CONCAT():
ALTER TABLE SupplierDetails
ADD practice AS LEFT(SupplierDescription + SupplierEmail, 300);
If you're using SQL Server 2012 and beyond:
ALTER TABLE SupplierDetails
ADD practice AS LEFT(CONCAT(SupplierDescription, SupplierEmail), 300);
Keep in mind that computed columns have to be persisted, that's why I'm adding LEFT(xxx, 300);, to make sure that your computed column won't be longer than 300 characters.
Please see this in action:
https://data.stackexchange.com/stackoverflow/query/429734/computed-column

Related

Migrate trigger Oracle to SQL Server

People,
I need migrate a Oracle trigger to SQL server, but I could not do.
The trigger in Oracle is very simple:
CREATE OR REPLACE TRIGGER trigger_teste
BEFORE INSERT OR UPDATE
ON teste
FOR EACH ROW
DECLARE
BEGIN
:new.id := (coalesce(:NEW.id, 0));
:new.vlr_sal := (coalesce(:NEW.vlr_sal, 0.00));
END;
I tried several ways but none successfully!
Thank for help!
My T-SQL is a bit rusty, but something like this should work. Note that SQL server does not have row level triggers, only statement level triggers.
CREATE TRIGGER trigger_teste
ON teste
BEFORE INSERT OR UPDATE
AS
update inserted
set id = coalesce(id, 0),
vlr_sal = coalesce(vlr_sal, 0.0)
GO
(Not sure if I got missed a semicolon or not. I never understood when SQL Server needs or deosn't need one)
See the manual for more details:
http://msdn.microsoft.com/en-US/library/ms189799%28v=sql.90%29
http://msdn.microsoft.com/en-US/library/ms191300%28v=sql.90%29
This is not an appropriate use of triggers in any flavour of RDBMS. The SQL standard allows us to define default values when we create the table using the DEFAULT constraint syntax. Both Oracle and SQL Server have this.
Obviously you haven't do this when you created the table. The good news is we can use ALTER TABLE to add default constraints. Something like this
alter table teste
alter column id set default 0
That's for SQL Server. In Oracle it would be:
alter table teste
modify id default 0
As the nameless equine points out, a complete replacement for the trigger must include NOT NULL constraints on the affected columns. If the existing table lacks not null constraints we can add them using the same syntax as shown above, replacing the DEFAULT clause with NOT NULL - or even combining the two clauses in the same statement.

Adding column between two other columns in SQL server

Can you add a column to a table inserting it in between two existing columns in SQL Server without dropping and re-creating the table?
Mediumly long answer, yes (ish) but it's ugly and you probably wouldn't want to do it.
please note: this code creates a physical table
CREATE TABLE MyTest (a int, b int, d int, e int)
INSERT INTO MyTest (a,b,d,e) VALUES(1,2,4,5)
SELECT * FROM MyTest
ALTER TABLE MyTest ADD c int
ALTER TABLE MyTest ADD d_new int
ALTER TABLE MyTest ADD e_new int
UPDATE MyTest SET d_new = d, e_new = e
ALTER TABLE MyTest DROP COLUMN d
ALTER TABLE MyTest DROP COLUMN e
EXEC SP_RENAME 'MyTest.d_new', 'd';
EXEC SP_RENAME 'MyTest.e_new', 'e';
SELECT * FROM MyTest
DROP TABLE MyTest
The simple answer is no. Is there a reason why column order is important to you?
Yes you can add here is the query for your concern:
ALTER table table_name ADD column column_name(new) datatype AFTER column_name
Take a look at this link:
http://www.bobsgear.com/display/ts/Adding+Column+After+Another+Column+-+SQL+Server+2005
As you can see, the answer is:
'not possible without moving data to a temp table'
which is what the SQL Server Management Studio actually does.
yes. You can drag and drop it in design mode, or copy and paste it in edit table mode
in response to comments, yes, this is with SQL Server Management Studio through the UI, and under the hood, it's doing the drop/recreate you're trying to avoid, so no, you can't do it without the server doing it for you.
This is possible in SQL Server Management Studio (SSMS).
Filter your table in Object Explorer and right click on the table > Design
Drag the arrow highlighted in the left to move your column.
First answer, no.
Second answer, according to this thread http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=58912, found via Can I logically reorder columns in a table?, you could create the column then reorder the columns in the table by editing the system tables. But it looks incredibly messy and I would not trust that things would not break.
For Mysql yes For SQL server No
also it's not a good practice to add column in between as it can hamper some queries if you are relying on the table schema in your project.

Is there any way to ALTER a column to switch off 'NOT NULL'?

Can this be done in bulk too? So that all columns in the table can be set to switch off the 'NOT NULL' flag?
You should be able to use an ALTER TABLE xxx ALTER COLUMN statement to redefine the column.
If this is a one-time thing you need to run, you could use a trick by writing a query that queries the column names for the table from the system/dba table and generates your alter statements. You copy the results of the query (your 15 or however many alter statements) into your script and just run that. I don't have much mssql experience nor an environment to test on right now but something along the lines of:
SELECT
'ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name + ' ' + data_type
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'xxx'
where you will need to manipulate the data_type part to add/remove the NULL constraint text
To do it in bulk, once...
Use SSMS designer to generate a script. This will rebuild your table (create a temp table, copy data, drop old table, rename temp table).
Otherwise, it's one at a time using ALTER TABLE...
Yes, you can do it. Read books online.
No, it can't be done in bulk, but you could execute several statements in a single query.
Get a list of the columns and a template that has the required SQL and use some tool to create the statements for you.
I have done this in Excel before, but you could write a real program using your language of choice.
When the number of tables is low enough I'm using SSMSE (SQL Server Management Studio Express), by entering design mode on each table and checking Allow Nulls on the required columns.
For a larger number of tables, try the answer provided by ChrisCM.

Changing column name in SQL Server 2000

Is there a way to change a column name in sql without having to recreate the table?
I've tried alter table dbo.Conforming rename column [xxx] to [xxx] and it doesn't work. any other ideas?
use sp_rename:
USE AdventureWorks;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO

How do I alter the precision of a decimal column in Microsoft SQL Server?

Is there a way to alter the precision of an existing decimal column in Microsoft SQL Server?
ALTER TABLE Testing ALTER COLUMN TestDec decimal(16,1)
Just put decimal(precision, scale), replacing the precision and scale with your desired values.
I haven't done any testing with this with data in the table, but if you alter the precision, you would be subject to losing data if the new precision is lower.
There may be a better way, but you can always copy the column into a new column, drop it and rename the new column back to the name of the first column.
to wit:
ALTER TABLE MyTable ADD NewColumnName DECIMAL(16, 2);
GO
UPDATE MyTable
SET NewColumnName = OldColumnName;
GO
ALTER TABLE CONTRACTS DROP COLUMN OldColumnName;
GO
EXEC sp_rename
#objname = 'MyTable.NewColumnName',
#newname = 'OldColumnName',
#objtype = 'COLUMN'
GO
This was tested on SQL Server 2008 R2, but should work on SQL Server 2000+.
ALTER TABLE (Your_Table_Name) MODIFY (Your_Column_Name) DATA_TYPE();
For you problem:
ALTER TABLE (Your_Table_Name) MODIFY (Your_Column_Name) DECIMAL(Precision, Scale);
In Oracle 10G and later following statement will work.
ALTER TABLE <TABLE_NAME> MODIFY <COLUMN_NAME> <DATA_TYPE>
If the current data type is NUMBER(5,2) and you want to change it to NUMBER(10,2), following is the statement
ALTER TABLE <TABLE_NAME> MODIFY <COLUMN_NAME> NUMBER(10,2)

Resources