Withdata Software provide some ETL (Extract-Transform-Load) tools for SQL Server: FileToDB Load TXT, CSV, TSV, XML, JSON, Excel, SQL, RDF, INI data to SQL Server DBToFile Export SQL Server data to TXT, CSV, TSV, XML, JSON, Excel, SQL files DBCopier Copy data between SQL Server and ... Read more
Category Archives: Sql Server
How to get index column names of a table from SQL Server
select i.name as IndexName, co.[name] as ColumnName from sys.indexes i join sys.objects o on i.object_id = o.object_id join sys.schemas s on s.schema_id = o.schema_id join sys.index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id join sys.columns co on co.object_id = ... Read more
How to get primary key of a table from SQL Server
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE OBJECTPROPERTY(OBJECT_ID(CONSTRAINT_SCHEMA + '.' + CONSTRAINT_NAME), 'IsPrimaryKey') = 1 AND TABLE_NAME = 'my_table_name' AND TABLE_SCHEMA = 'my_schema_name' See also: How to get primary key of a table from PostgreSQL How to get ... Read more
How to execute sql file at SQL Server command line
You can use sqlcmd to run a Transact-SQL script file. A Transact-SQL script file is a text file that can contain a combination of Transact-SQL statements, sqlcmdcommands, and scripting variables. To run the script file 1.Open a command prompt window. 2.In the Command Prompt window, ... Read more
How to create a user in SQL Server Express database
Create a SQL Authenticated login first with CREATE LOGIN, then add a user associated with that login to your database by using CREATE USER. USE [master] GO CREATE LOGIN [YourUsername] WITH PASSWORD=N'YourPassword', DEFAULT_DATABASE=[YourDB], CHECK_EXPIRATION=OFF, ... Read more
The SQL Server equivalent for the MySQL Limit
In MySQL, you can use “Limit n,m”, like this: select * from sometable order by name limit 20,10 And in SQL Server, use this query: SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY name) as rowNum FROM sometable ) sub WHERE rowNum > 20 AND ... Read more
How to generate a CREATE TABLE statement for a given table in SQL Server
declare @table varchar(100) set @table = 'my_table' -- set table name here declare @sql table(s varchar(1000), id int identity) -- create statement insert into @sql(s) values ('create table [' + @table + '] (') -- column list insert into @sql(s) select ' ['+column_name+'] ' + ... Read more
Replace (Update/Insert) a row into SQL Server table
In MySQL, we use “ON DUPLICATE KEY UPDATE” to either updates or inserts a row in a table. How to do it in SQL Server? Just like this: if not exists (select 1 from employees where employee_id = 1) insert into employees (employee_id,last_name,first_name) values ( 1,'smith', ... Read more
Remove duplicate rows in SQL Server
Assuming no nulls, you GROUP BY the unique columns (eg. col_1, col_2, col_3), and SELECT the MIN (or MAX) Row ID (eg. row_id) as the row to keep. Then, delete everything that didn’t have a row id: DELETE my_table FROM my_table LEFT OUTER JOIN ( SELECT MIN(row_id) as row_id, col_1, ... Read more
How to get column names from Sql Server table
SELECT [name] AS [Column Name] FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = ‘U’ AND [Name] = ‘My_Table_Name’) Type = ‘V’ for views Type = ‘U’ for tables For SQL Server 2008, we can use information_schema.columns for getting ... Read more