Converting Select results into Insert script – SQL Server [closed]

Here is another method, which may be easier than installing plugins or external tools in some situations: Do a select [whatever you need]INTO temp.table_namefrom [… etc …]. Right-click on the database in the Object Explorer => Tasks => Generate Scripts Select temp.table_name in the “Choose Objects” screen, click Next. In the “Specify how scripts should … Read more

optional parameters in SQL Server stored proc?

You can declare like this CREATE PROCEDURE MyProcName @Parameter1 INT = 1, @Parameter2 VARCHAR (100) = ‘StringValue’, @Parameter3 VARCHAR (100) = NULL AS /* check for the NULL / default value (indicating nothing was passed */ if (@Parameter3 IS NULL) BEGIN /* whatever code you desire for a missing parameter*/ INSERT INTO …….. END /* … Read more

Real life example, when to use OUTER / CROSS APPLY in SQL

Some uses for APPLY are… 1) Top N per group queries (can be more efficient for some cardinalities) SELECT pr.name, pa.name FROM sys.procedures pr OUTER APPLY (SELECT TOP 2 * FROM sys.parameters pa WHERE pa.object_id = pr.object_id ORDER BY pr.name) pa ORDER BY pr.name, pa.name 2) Calling a Table Valued Function for each row in … Read more

How can I create a unique constraint on my column (SQL Server 2008 R2)?

Set column as unique in SQL Server from the GUI: They really make you run around the barn to do it with the GUI: Make sure your column does not violate the unique constraint before you begin. Open SQL Server Management Studio. Right click your Table, click “Design”. Right click the column you want to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)