sql-server-2005

How to insert table values from one database to another database?

点点圈 提交于 2019-12-17 10:16:37
问题 I want a query to insert records from one table to another table in a different database if the destination table already exists, it should append the records at the end of the table. 回答1: How about this: USE TargetDatabase GO INSERT INTO dbo.TargetTable(field1, field2, field3) SELECT field1, field2, field3 FROM SourceDatabase.dbo.SourceTable WHERE (some condition) 回答2: How to insert table values from one server/database to another database? 1 Creating Linked Servers {if needs} (SQL server

SQL Server 2005 - Export table programmatically (run a .sql file to rebuild it)

余生颓废 提交于 2019-12-17 10:15:52
问题 I have a database with a table Customers that have some data I have another database in the office that everything is the same, but my table Customers is empty How can I create a sql file in SQL Server 2005 (T-SQL) that takes everything on the table Customers from the first database, creates a, let's say, buildcustomers.sql, I zip that file, copy it across the network, execute it in my SQL Server and voila! my table Customers is full How can I do the same for a whole database? 回答1: This

C# SqlCommand - cannot use parameters for column names, how to resolve?

心已入冬 提交于 2019-12-17 09:59:50
问题 Is there any way how to do that? This does not work: SqlCommand command = new SqlCommand("SELECT @slot FROM Users WHERE name=@name; "); prikaz.Parameters.AddWithValue("name", name); prikaz.Parameters.AddWithValue("slot", slot); The only thing I can think of is to use SP and declare and set the variable for the column. Seems to me a bit ackward. 回答1: You cannot do this in regular SQL - if you must have configurable column names (or table name, for that matter), you must use dynamic SQL - there

How do I expand comma separated values into separate rows using SQL Server 2005?

巧了我就是萌 提交于 2019-12-17 09:58:22
问题 I have a table that looks like this: ProductId, Color "1", "red, blue, green" "2", null "3", "purple, green" And I want to expand it to this: ProductId, Color 1, red 1, blue 1, green 2, null 3, purple 3, green Whats the easiest way to accomplish this? Is it possible without a loop in a proc? 回答1: I arrived this question 10 years after the post. SQL server 2016 added STRING_SPLIT function. By using that, this can be written as below. declare @product table ( ProductId int, Color varchar(max) )

FOR XML PATH(''): Escaping “special” characters

假装没事ソ 提交于 2019-12-17 09:43:31
问题 This code basically translates characters based on position in one string to the character at the same position in another string and it runs for all rows in the table. When I run this (simplified version): DECLARE @R char(40) DECLARE @U char(40) SET @R=' abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+'+char(181) SET @U=REVERSE(@R) DECLARE @TestTable TABLE (RowID int identity(1,1) primary key, Unreadable varchar(500)) INSERT INTO @TestTable VALUES ('+µt$zw!*µsu+yt!+s$xy') INSERT INTO @TestTable VALUES

Determine what user created objects in SQL Server

删除回忆录丶 提交于 2019-12-17 09:37:30
问题 I'm trying to go through our development DB right now and clean up some of the old test procs/tables. Is it possible to determine what user created objects in a SQL Server 2005 database? If so, how would I go about finding that information? Edit: Just wanted to clarify that the objects in question already exist. Setting up auditing and triggers probably wouldn't do me much good. I guess I was mostly looking for a way to use the system tables/views to get to the information. 回答1: The answer is

SQL Server: How to permission schemas?

ぐ巨炮叔叔 提交于 2019-12-17 09:22:22
问题 Inspired by various schema related questions I've seen... Ownership chaining allows me to GRANT EXECUTE on a stored procedure without explicit permissions on tables I use, if both stored procedure and tables are in the same schema. If we use separate schemas then I'd have to explicitly GRANT XXX on the the different-schema tables. The ownership chaining example demonstrates that. This means the stored proc executing user can read/write your tables directly. This would be like having direct

SQL Server: How to permission schemas?

浪尽此生 提交于 2019-12-17 09:21:09
问题 Inspired by various schema related questions I've seen... Ownership chaining allows me to GRANT EXECUTE on a stored procedure without explicit permissions on tables I use, if both stored procedure and tables are in the same schema. If we use separate schemas then I'd have to explicitly GRANT XXX on the the different-schema tables. The ownership chaining example demonstrates that. This means the stored proc executing user can read/write your tables directly. This would be like having direct

Unable to begin a distributed transaction

大城市里の小女人 提交于 2019-12-17 08:16:12
问题 i'm trying to run SQL against a linked server, but i get the errors. BEGIN DISTRIBUTED TRANSACTION SELECT TOP 1 * FROM Sessions OLE DB provider "SQLNCLI" for linked server "ASILIVE" returned message "No transaction is active.". Msg 7391, Level 16, State 2, Line 3 The operation could not be performed because OLE DB provider "SQLNCLI" for linked server "ASILIVE" was unable to begin a distributed transaction. There are two errors returned by the provider: Error #1: Number: $80040E14 Source:

OPENXML with xmlns:dt

荒凉一梦 提交于 2019-12-17 07:51:12
问题 Use OPENXML to get dt element in MSSQL 2005. How can I get xmlns:dt element in xml? For example, get a result set of two rows that list product id and country code. 121403 GBR 121403 USA declare @xmldata xml set @xmldata = '<?xml version="1.0"?> <data xmlns="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes"> <products> <product> <product_id><![CDATA[121403]]></product_id> <countries> <dt:country>GBR</dt:country> <dt:country>USA</dt:country> </countries> <