Showing posts with label MS SQL. Show all posts
Showing posts with label MS SQL. Show all posts

Monday, July 30, 2012

MSSQL 2008 : Cannot open database requested by the login. The login failed

I got this error:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "ICBF_GDOC_TESTS" requested by the login. The login failed. (4060) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "ICBF_GDOC_TESTS" requested by the login. The login failed. (4060)') None None.

In my case, the issue was that the database name was wrong , that was all :/

Thursday, July 19, 2012

SQL Server: update column with sequential value

I have a database table that has a lot of data already in the table and I need to add a new column to this table to include a new sequential number.  In addition to adding the column I also need to populate the existing records with an incremental counter.


So I can do this by doing the following:


// adds new columns
ALTER TABLE [dbo].[d_expedientes] ADD [type_expediente] [int] default 0;


// set the value of the new column to 1,2,3,4,5,...

DECLARE @type_expediente INT 
SET @type_expediente = 0 
UPDATE d_expedientes
SET @type_expediente = type_expediente = @type_expediente + 1 


Reference:
http://www.mssqltips.com/sqlservertip/1467/populate-a-sql-server-column-with-a-sequential-number-not-using-an-identity/

Wednesday, July 4, 2012

MSSQL 2008: Make and Restore a Database BackUp

Here are the steps, in spanish ;)

1) Ingresar al menú de MS SQL Server, opción Enterprise Manager
2) Ingresar al servidor origen y seleccionar la base de datos a copiar
3) Presionar clic derecho del ratón sobre la base de datos, en la opción "All Tasks" seleccionar "Backup Database"
4) Se presentará una ventana con titulo SQL Server Backup
5) Selecciona en el área de "Backup" la opción de "Database Complete"
6) En el área de "Destination" presionar el botón de "Add" y se presentará una ventana con el titulo de "Select Backup Destination"
7) Seleccionar "Backup Device", y en la lista de opciones seleccionar la opción de "New backup device" y presionar el botón de "OK", se presentará una ventana con titulo "Backup Device Properties", ingresas el nombre y seleccionas el directorio donde se almacenará en archivo (el nombre se coloca automáticamente)
8) Después regresas a la primera ventana que se presento (SQL Server Backup) y presionar el botón de "OK"
9) Copias ese archivo al servidor destino,
10) Seleccionar la opción MS SQL Server - Enterprise Manager
11) Presionar clic derecho del ratón sobre la base de datos, en la opción "All Tasks" seleccionar "Restore Database"
12) En la opción "General", seleccionar la base de datos a restaurar en el control "Restore as database", esta opción esta por defecto
13) En el área "Restore" de la opción "General", seleccionar la opción "From Device", y seleccionar el archivo donde se realizo la copia de seguridad del servidor origen
14) Seleccionar la opción "Options",
15) Por defecto se crean las rutas de la base de datos origen, para modificarlas es necesario seleccionar la opción "Force restore ver existing database"
16) Presionar el botón de "OK"

Resource:
http://www.todoexpertos.com/categorias/tecnologia-e-internet/bases-de-datos/sql-server/respuestas/397691/copiar-base-de-datos

Saturday, December 17, 2011

Tuesday, October 11, 2011

MSSQL Concat string


SELECT FirstName + ' ' + LastName As FullName FROM Customers
Source:
http://www.sqlbook.com/SQL/SQL-CONCATENATE-24.aspx

MSSQL: UNION in query

SELECT * FROM Table1
UNION
SELECT * FROM Table2

Note: the items called in the select sentence have to be the same quantity 

Source:
http://msdn.microsoft.com/es-es/library/ms191141.aspx

Wednesday, September 21, 2011

MS SQL 2005: varchar to datetime - La conversión del tipo de datos varchar en datetime produjo un valor fuera de intervalo error.

I was trying to execute this:

update d_entrada set fecha_radicado = cast('2011-09-21 20:41:06.000' as datetime)  where id = '123456';


and I got this error

La conversión del tipo de datos varchar en datetime produjo un valor fuera de intervalo error.

I put the datetime this way ('2011-09-21 20:41:06.000') because when I execute

select fecha_radicado from d_entrada where id = '123456';

I get

2011-09-21 20:41:06.000

But to make the update query work propertly I had to switch 09 and 21 date values:

update d_entrada set fecha_radicado = cast('2011-21-09 20:41:06.000' as datetime)  where id = '123456';


then I got:



(1 row(s) affected)


Monday, August 29, 2011

MS SQL: query to add column

ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

i.e:

ALTER TABLE Protocols ADD ProtocolTypeID int NOT NULL DEFAULT(1);

Wednesday, June 1, 2011

MS SQL compare string CASE INSENSITIVE / SENSITIVE

-- case insensitive
SELECT UserId, email
FROM aspnet_membership
WHERE email = 'biilg@microsoft.com' COLLATE SQL_Latin1_General_CP1_CI_AS

-- case sensitive
SELECT UserId, email
FROM aspnet_membership
WHERE email = 'billg@microsoft.com' COLLATE SQL_Latin1_General_CP1_CS_AS

Source:
http://aspadvice.com/blogs/ssmith/archive/2007/09/30/Case-Sensitive-or-Insensitive-SQL-Query.aspx