Caracteres extraños (cuadraditos) en nvarchar SQLServer

Filed Under (SQLServer) by admin on 06-05-2010

Tagged Under : , ,

Tenemos un campo nvarchar(1) de la base de datos que se introduce con un carácter extraño, modificamos la carga y se introducían bien pero había que cambiar las ya existentes. Para ello se pensó en muchas cosas (codificación, tamaño de la cadena, etc) dando al final con esta solución:

Problema:
cuadraditos

Solución:
UPDATE Esquema.Tabla
SET Campo = 'valorquequeremos'
WHERE ASCII(Campo) = 0

Unir el resultado de dos SQL: Operador UNION

Filed Under (SQLServer) by admin on 23-03-2010

Tagged Under : , , ,

La sintaxis es la siguiente:

[Instrucción SQL 1]
UNION
[Instrucción SQL 2]

Supongamos que tenemos las siguientes dos tablas,

Tabla Store_Information
store_name Sales Date
Los Angeles 1500 € 05-Jan-1999
San Diego 250 € 07-Jan-1999
Los Angeles 300 € 08-Jan-1999
Boston 700 € 08-Jan-1999

Tabla Internet_Sales
Date Sales
07-Jan-1999 250 €
10-Jan-1999 535 €
11-Jan-1999 320 €
12-Jan-1999 750 €

y deseamos saber de todas las fechas donde hay una operación de venta. Para hacerlo, utilizamos la siguiente instrucción SQL:

SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales

Resultado:

Date
05-Jan-1999
07-Jan-1999
08-Jan-1999
10-Jan-1999
11-Jan-1999
12-Jan-1999

Lo único que hay que tener en cuenta es que los campos que queremos unir sean del mismo tipo en ambas consultas a la vez de que tengan el mismo nombre.

SQL cambiar tabla de esquema en SQL Server 2005

Filed Under (SQLServer) by admin on 22-03-2010

ALTER SCHEMA Comisiones TRANSFER dbo.GestoresComerciales;
GO

Con esta consulta cambiamos la tabla dbo.GestoresComerciales del esquema “dbo” al esquema “Comisiones”

Más información en: http://msdn.microsoft.com/es-es/library/ms173423.aspx

“Saltarse” las Constraints en SQLServer

Filed Under (.Net, SQLServer) by admin on 01-12-2009

Tagged Under : , , ,

Las constraints de una tabla de SQL Server se pueden desactivar temporalmente con la opción NOCHECK CONSTRAINT ALL de ALTER TABLE.
El comando quedaría así:
ALTER TABLE tabla NOCHECK CONSTRAINT ALL;
Con esto se desactivan y puedes hacer la carga de datos sin que te molesten las FK

Después, para volver a activarlas sólo tienes que hacer este otro ALTER TABLE:
ALTER TABLE tabla WITH CHECK CHECK CONSTRAINT ALL;
Reactiva las constraints chequeando que los datos de las tablas las cumplan

Si quieres volver a habilitarlas sin validar los datos que hayas cargado, aunque no es muy recomendable también se puede hacer:
ALTER TABLE tabla CHECK CONSTRAINT ALL;

Entrar en modo administrador en SQL Server 2005

Filed Under (.Net, SQLServer) by admin on 09-11-2009

Tagged Under : , , ,

Casi todos tenemos el SQL Server con la licencia de 2 usuarios y muchas veces algun compañero se le olvida cerrar la sesión mientras se va ha almorzar… pues bien… entra por fuerza bruta¡¡

ejecuta: mstsc /admin

y zas¡¡ dentro en modo administrador… despues puedes entrar en las tareas del servidor y ver quien esta conectado para cerrar las sesiones que veas.

Linq to SQL Debug Visualizer, depura facilmente tus consultas LINQ to SQL

Filed Under (.Net, LINQ, SQLServer) by admin on 01-07-2009

Tagged Under : , ,

Using the LINQ to SQL Debug Visualizer

One of the nice development features that LINQ to SQL supports is the ability to use a “debug visualizer” to hover over a LINQ expression while in the VS 2008 debugger and inspect the raw SQL that the ORM will ultimately execute at runtime when evaluating the LINQ query expression.

For example, assume we write the below LINQ query expression code against a set of data model classes:

We could then use the VS 2008 debugger to hover over the “products” variable after the query expression has been assigned:

And if we click the small magnifying glass in the expression above, we can launch the LINQ to SQL debug visualizer to inspect the raw SQL that the ORM will execute based on that LINQ query:

If you click the “Execute” button, you can even test out the SQL query and see the raw returned results that will be returned from the database:

This obviously makes it super easy to see precisely what SQL query logic LINQ to SQL ORM is doing for you. 

You can learn even more about how all this works by reading the Part 3: Querying our Database segment in my LINQ to SQL series above.

How to Install the LINQ to SQL Debug Visualizer

The LINQ to SQL Debug Visualizer isn’t built-in to VS 2008 – instead it is an add-in that you need to download to use.  You can download a copy of it here.

The download contains both a binary .dll assembly version of the visualizer (within the \bin\debug directory below), as well as all of the source code for the visualizer:

To install the LINQ to SQL debug visualizer, follow the below steps:

1) Shutdown all running versions of Visual Studio 2008

2) Copy the SqlServerQueryVisualizer.dll assembly from the \bin\debug\ directory in the .zip download above into your local \Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers\ directory:

3) Start up Visual Studio 2008 again.  Now when you use the debugger with LINQ to SQL you should be able to hover over LINQ query expressions and inspect their raw SQL (no extra registration is required).