Posts Tagged ‘SQL’

SQL Alter a column with a default value

Tuesday, February 9th, 2010

If a column has a default value it means there is a constraint on that column setting it the default value.  The alter command will give you an error like this:

the object is dependent on column ..

I made a script for altering several columns in different tables, I had to alter them from float to decimal, and all of them had default value=0.

DECLARE @col nvarchar(100)
DECLARE @tabel nvarchar(100)
DECLARE @constr nvarchar(100)
declare @sql nvarchar(500)

DECLARE cTemp CURSOR LOCAL FOR select t_obj.name as TABLE_NAME
,col.name as COLUMN_NAME
,c_obj.name as CONSTRAINT_NAME
from sysobjects c_obj
join syscomments com on c_obj.id = com.id
join sysobjects t_obj on c_obj.parent_obj = t_obj.id
join    sysconstraints con on c_obj.id = con.constid
join syscolumns col on t_obj.id = col.id
and con.colid = col.colid where col.name=’col1′ or col.name=’col2′  –etc

OPEN cTemp

FETCH NEXT FROM cTemp INTO @tabel, @col, @constr
WHILE @@FETCH_STATUS = 0
BEGIN

SET @sql = ‘ALTER TABLE ‘+@tabel+’ DROP CONSTRAINT ‘+ @constr
PRINT @sql
EXEC sp_executesql @sql

SET @sql = ‘ALTER TABLE ‘+@tabel+’ ALTER COLUMN ‘+@col+’ DECIMAL(14, 6)’
PRINT @sql
EXEC sp_executesql @sql

SET @sql=’alter table ‘+@tabel+’ ADD CONSTRAINT ‘+@constr+’  DEFAULT 0 FOR ‘+ @col
PRINT @sql
EXEC sp_executesql @sql

FETCH NEXT FROM cTemp INTO @tabel, @col, @constr
END
CLOSE cTemp
DEALLOCATE cTemp

This script will drop the constraints for these columns, alter the columns and then rebuild the constraints. In the select from the cursor you can include the column with the default value, if you need to modify the script for columns with different default values.

Good luck

Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file

Friday, January 8th, 2010

I had this error when I tried to export some data from SQL Server 2008 with BCP utility:

Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file

The problem was that I tried to export to the path “C:\export.dat”, I tried also to export to “D:\export.dat”, but the error persisted.

The solution to this problem is to set the destination file to “C:\folder\export.dat”.

Al least this worked for me. Hope it helps. Good luck!

SQL 2008 install error: ” is not a valid login or you do not have permission.

Wednesday, October 21st, 2009

I got this error when I tried to install SQL Server 2008: ” is not a valid login or you do not have permission.

The problem behind this error is that the name of the computer was the same as the windows username. Change the name of the computer and it will work.

How to find out SQL Server version?

Monday, September 7th, 2009

You can find out the SQL Server version using the following queries:

set nocount on
go

select @@version
go

select ‘Edition: ‘ + convert(char(30), serverproperty(‘Edition’))
go

select ‘Product Version: ‘ + convert(char(20), serverproperty(‘ProductVersion’))
go

select ‘Product Level: ‘ + convert(char(20),serverproperty(‘ProductLevel’))
go

set nocount off
go

Clear MS SQL log file

Monday, August 17th, 2009

SP_helpfile

–DBCC ShrinkFile(infocont_apoteca3_log, 50)

To clear a log file, first I suggest to back-up the database and then execute this script, witch will shrink your logfile to 50 MB:

DBCC ShrinkFile(mydatabase_log, 50)

where mydatabase_log is the name of the logfile. You can obtain the name of the log by executing:

SP_helpfile

Repair SQL database from Sql Studio Management

Monday, August 10th, 2009

EXEC sp_resetstatus ‘mydatabase’;
ALTER DATABASE mydatabase SET EMERGENCY
DBCC checkdb(‘mydatabase’)
ALTER DATABASE mydatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB (‘mydatabase’, REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE mydatabase SET MULTI_USER

Select all columns from all tables in the database

Friday, June 19th, 2009

If you want to see all the columns and their properties write the following SQL command in SQL Studio Management:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

You can filter the results by table name, data type, collation, etc.