Changing column name in SQL Server 2000 - sql-server

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

Related

SQL Server: Rename column after dropping another one without GO

Is this the right way to make sure that a column of my table is only renamed after another column is dropped?
ALTER TABLE mytbl DROP COLUMN tmpcol
EXEC sp_rename 'mytbl.tmpcol2', 'tmpcol', 'COLUMN'
I'm not allowed to use the "GO"-separator.
I've tested the above two lines for a bunch of different table-sizes.
It worked as I expected, i.e. the 2nd Line is only executed after the 1st one.
But how can I make sure, that this will be the preferred execution plan for any table?
Is this guaranteed by EXEC ?
SQL Server run query line-by-line, there is no chance that EXEC can run before ALTER TABLE

What's wrong with my `ALTER TABLE` statement?

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

Truncate all tables of a database in SQL Server 2005

How can I truncate all tables of a database?
Why would you want to truncate all tables? If you want an empty database, why not run the CREATE script of the database?
If you want to Truncate a table referenced by a foreign key, you will have to drop the FK constraint first. Disabling constraints is something that is not possible anymore in recent versions of SQL Server.
You can see this post : how-do-you-truncate-all-tables-in-a-database-using-tsql
I use the script
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
Reset Auto-Increment? I'm not sure if you understand correctly how this works.
Primary Key incrementing is handled by SQL Server using the IDENTITY specification. If your tables have got no data in them, it will always start from 0.
If I were you, I'd go have a flick through your programming books and pick up some basic database knowledge as it sounds like you're missing some fundamental facts there.

How do i recreate a trigger in SQL Server?

i use the statement drop trigger if exist TRIGGER in sqlite but sql server doesnt like the if statement. (i guess exist is the offending word). I do this right next to my create trigger statement because i want to drop older triggers with the same name so i can replace it with this new one.
How do i do this in SQL server?
in SQL Server Management Studio (and, I think in Query Analyzer) right-click the trigger in the explorer, and choose the Script-as option, choose 'Drop Trigger' in clipboard, and SSMS will create the T-SQL syntax for you to drop that trigger.
Sorry I haven't given you T-SQL you can copy and paste, but this way you'll know how to do it for next time.
You can check for the existence of a specific Trigger like so.
IF EXISTS
(
select name
from sys.objects
where type='TR' and name ='Trigger Name'
)
BEGIN
--Add your Trigger create code here
END
I find this to be a more compact SQL Server equivalent to MySQL's DROP TRIGGER IF EXISTS syntax:
IF OBJECT_ID('XXXX', 'TR') IS NOT NULL
DROP TRIGGER XXXX
I'd use something like:
IF objectproperty(object_id('dbo.xxx'), 'isTrigger') = 1
DROP PROCEDURE dbo.xxx
GO
CREATE TRIGGER dbo.xxx [etc]
replacing xxx with your trigger name (and dbo with the relevant schema, if necessary).
Alternatively, you could just use
ALTER TRIGGER dbo.xxx [etc]
Since version 2016 this syntax is also supported by Microsoft SQL Server:
DROP TRIGGER IF EXISTS trigger_name

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