sql-server-2005

SQL and multiple statements in stored procedure

故事扮演 提交于 2020-01-03 17:49:10
问题 I'm working on SQL server 2005 and I have a very simple stored procedure: create PROCEDURE [dbo].[tblTabel_Insert] @ID int, @Code nvarchar(50) = null AS SET NOCOUNT ON; IF EXISTS (SELECT ID, code FROM tblTabel WHERE ID = @ID and code = @Code) UPDATE tblTabel SET ID = @ID,code = @Code WHERE ID = @ID ELSE BEGIN INSERT INTO tblTabel (ID,code) VALUES ( @ID ,@Code); END My question is: is it posible to have multiple queries in my stored procedure ? I want to add the lines UPDATE tblTabelB SET ID =

how to restore using restore class of Microsoft.SqlServer.Management.Smo namespace

人走茶凉 提交于 2020-01-03 17:45:27
问题 public void RestoreDatabase(String databaseName, String filePath, String serverName, String userName, String password, String dataFilePath, String logFilePath) { Restore sqlRestore = new Restore(); BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File); sqlRestore.Devices.Add(deviceItem); sqlRestore.Database = databaseName; ServerConnection connection = new ServerConnection(serverName, userName, password); Server sqlServer = new Server(connection); Database db =

SQL Server parallels to Oracle DBMS_METADATA.GET_DDL?

橙三吉。 提交于 2020-01-03 17:12:19
问题 I'm looking for command line or scripted solutions to pull the DDL out of SQL Server 2005+ for all database objects: tables, stored procs, views, indices/indexes, constraints, etc. GUI tools are not of interest. Preference is for built-in tools, since that would be the most comparable to Oracle's DBMS_METADATA stuff. Also, preference for a solution that is as simple as Oracle's for getting the DDL out - eg, a one liner: SELECT DBMS_METADATA.GET_DDL('TABLE', 'MY_TABLE') FROM DUAL Note: Getting

Is there an Access equivalent of the SQL Server NewId() function?

◇◆丶佛笑我妖孽 提交于 2020-01-03 12:03:23
问题 I have written SQL statements (stored in a text document) that load data into a SQL Server database. These statements need to be repeated daily. Some of the statements use the NewId() function to populate a keyed field in the database, and this works fine. While I'm in the process of writing an application to replicate these statements, I want to use Access queries and macros instead of copying and pasting queries into SQL Server, thus saving me time on a daily basis. All is working fine but

When should I create database indexes? [duplicate]

我与影子孤独终老i 提交于 2020-01-03 08:18:06
问题 This question already has answers here : When should you consider indexing your sql tables? (8 answers) Index all columns (5 answers) Closed last year . When to set index for a table, during table creation or on performance tuning? What are the advantages and disadvantages of indexing? 回答1: Many (most?) DBMS use indexes to support unique constraints. Always create indexes to enforce unique constraints; they (the constraints) are crucial to the correct operation of your database. Where you

SQL Server - pull X random records per state

时光怂恿深爱的人放手 提交于 2020-01-03 08:14:06
问题 I have a table with records for each zip code in the united states. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing this? 回答1: Use: WITH sample AS ( SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.state ORDER BY NEWID()) AS rank FROM ZIPCODES t) SELECT s.* FROM sample s WHERE s.rank <= 5 回答2: SELECT * FROM ZipCodes ORDER BY NEWID() 来源: https://stackoverflow.com/questions/3998573/sql-server-pull-x-random-records-per-state

SQL Server - pull X random records per state

泄露秘密 提交于 2020-01-03 08:14:05
问题 I have a table with records for each zip code in the united states. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing this? 回答1: Use: WITH sample AS ( SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.state ORDER BY NEWID()) AS rank FROM ZIPCODES t) SELECT s.* FROM sample s WHERE s.rank <= 5 回答2: SELECT * FROM ZipCodes ORDER BY NEWID() 来源: https://stackoverflow.com/questions/3998573/sql-server-pull-x-random-records-per-state

BULK INSERT into specific columns?

时光总嘲笑我的痴心妄想 提交于 2020-01-03 07:26:09
问题 I want to bulk insert columns of a csv file to specific columns of a destination table. Description - destination table has more columns than my csv file. So, I want the csv file columns to go to the right target columns using BULK INSERT. Is this possible ? If yes, then how do I do it ? I saw the tutorial and code at - http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/ and http://www.codeproject.com

NUnit Rollback attribute seems to fail on SQL Server 2005

怎甘沉沦 提交于 2020-01-03 05:27:06
问题 I'm using a nice little [Rollback] attribute on my NUnit database-level tests: public class RollbackAttribute : Attribute, ITestAction { private TransactionScope _transaction; public ActionTargets Targets { get { return ActionTargets.Test; } } public void BeforeTest(TestDetails testDetails) { _transaction = new TransactionScope(); } public void AfterTest(TestDetails testDetails) { _transaction.Dispose(); } } So I can then decorate my db-based tests like this: [Test] [Rollback] public void

How to insert value to table using cursor in stored procedure without getting duplicate records?

倾然丶 夕夏残阳落幕 提交于 2020-01-03 04:50:50
问题 I am trying to insert records into existing but empty table based on from date and to date parameters using cursor in stored procedure. Please let me know what I am doing wrong in the below SQL? When executing this procedure I am getting first row duplicated multiple times. Error: Maximum stored procedure, function, trigger or view nesting level exceeded (limit32) Code: ALTER proc [dbo].[spempmaster] (@date1 datetime,@date2 datetime) as Begin Set nocount on declare @doj datetime declare