Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all 3160 articles
Browse latest View live

Simulating LAG And LEAD Prior To SQL Server 2012

$
0
0

Last month I covered a Special Islands challenge . The task was to identify periods of activity for each service ID, tolerating a gap of up to an input number of seconds ( @allowedgap ). The caveat was that the solution had to be pre-2012 compatible, so you couldn’t use functions like LAG and LEAD, or aggregate window functions with a frame. I got a number of very interesting solutions posted in the comments by Toby Ovod-Everett, Peter Larsson, and Kamil Kosno. Make sure to go over their solutions since they’re all quite creative.

Curiously, a number of the solutions ran slower with the recommended index than without it. In this article I propose an explanation for this.

Even though all solutions were interesting, here I wanted to focus on the solution by Kamil Kosno, who’s an ETL developer with Zopa. In his solution, Kamil used a very creative technique to emulate LAG and LEAD without LAG and LEAD. You will probably find the technique handy if you need to perform LAG/LEAD-like calculations using code that is pre-2012 compatible.


SSIS SharePoint Source

$
0
0
This post shows you how to use Office 365 SharePoint as data source in SSIS Data Flow.
Pre-requisite:
1. You already have basic knowledge on SSIS.

2. You have installed SharePoint SDK to your development environment https://chanmingman.wordpress.com/2018/10/07/ssis-could-not-load-file-or-assembly-microsoft-sharepoint-client/

3. SQL Server Data Tool is installed https://chanmingman.wordpress.com/2014/08/02/sql-server-data-tool-ssdt/ .

4. You have Office 365 SharePoint online login.
It used to have a codeplex SharePoint data source but it just didn’t last for very long. A lot of people are still looking for a SSIS SharePoint data source. In this post, I will show you how to create a SSIS SharePoint Source with the combination of SharePoint SDK and Script Component in SSIS.
1. Create a SSIS project.
2. Drag Data Flow Task to Control Flow page. Double click the Data Flow Task. Drag Script Component to Data Flow page. When Select Script Component Type pops up then select Source .
SSIS SharePoint Source

3. Double click the Script Component. Click on Inputs and Outputs.


SSIS SharePoint Source

4. Expand the Output 0 .Click the Add Column . In this example, I will add 2 output columns, CategoryIdOut and ProductNameOut.

5. Click the Script . Click Edit Script…


SSIS SharePoint Source
6. Replace the CreateNewOutputRows method with the following code. Of course, you need to change the siteUrl, password, and login name.
public
override
void CreateNewOutputRows()
{

string siteUrl = “ https://ming.sharepoint.com/sites/dev&#8221 ; ;

ClientContext clientContext = new ClientContext(siteUrl);
string password = “password” ;
SecureString securePassword = new SecureString();
foreach ( char c in password)
{
securePassword.AppendChar(c);
}
clientContext.Credentials = new SharePointOnlineCredentials( “ming@ming.onmicrosoft.com” , securePassword);
SP.List oList = clientContext.Web.Lists.GetByTitle( “ProductList” );
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = “<View><RowLimit>100</RowLimit></View>” ;
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem,
items => items.Include(
item => item.Id,
item => item[ “CategoryId” ],
item => item[ “ProductName” ]));
clientContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
Console.WriteLine( “ID: {0} \nId: {1} \nName: {2}” , oListItem, oListItem.Id, oListItem[ “ProductName” ]);
Output0Buffer.AddRow();
Output0Buffer.ProductNameOut = oListItem[ “ProductName” ].ToString();
Output0Buffer.CategoryIdOut = oListItem[ “CategoryId” ].ToString();
}
}
7. Finally drag a OLE DB destination. For me, I am using the Northwind database, Products table.
SSIS SharePoint Source

SDU Tools: Is XACT_ABORT on in my SQL Server session?

$
0
0

XACT_ABORTis one of the least well understood options that you can configure in a SQL Server session. Yet it's very important. XACT_ABORT makes statement-terminating errors become batch-terminating errors. Without it, even within a transaction, many errors only terminate the statement that they occur in, and control passes to the next statement within the transaction, not out of the transaction.

In nearly every stored procedure that I write, the template includes the following lines:

SET XACT_ABORT ON;

SET NOCOUNT ON;

In our free SDU Tools for developers and DBAs, we added a function IsXactAbortOn to let you determine in code, if it's enabled.

You can see the outcome in the main image above.

You can see it in action here:

To become an SDU Insider and to get our free tools and eBooks, please just visit here:

http://sdutools.sqldownunder.com

SQL Server Sorting Algorithm

$
0
0
Sort javascript to match SQL Server sort

Can anyone point me towards a sorting algorithm in javascript that would sort the same way SQL Server does (for nvarchar/unicode columns)? For reference, my previous question about this behavior can be found here: SQL Server 2008 - different sort ord

SQL Server: Sorting and Verifying

I have a table named Logs: ->OCCUR_TIME --date and time ->NAME --name of a person ->KIND --the kind of log (eg. 40 means `something`) ->VALUE --the value of the kind of log (eg. 99) I have to create a query: SELECT * FROM LOGS WHERE NAME='dude

SQL Server - Sort by datepart week

I am using SQL Server. When I try to sort information by week using the datepart function it returns no values. There are values in my table for the selected date range. The query I am trying to use is: Declare @StartDate datetime, @EndDate datetime

UPDATE SQL Server Statement Algorithm

Suppose I have two tables. Table 1: ID NAME 1 'AAA' 2 'BBB' Table 2: ID IDNAME 1 'CCC' 1 'DDD' I have to update first table values so I am using below query: UPDATE a SET a.name = b.IDname FROM table1 a JOIN table2 b ON a.ID = b.id Now, my question i

SQL Server: sorting null values from date to last line

I have a problem here in sorting of date that has some of entries are null I want the null values to be in the last row. I've already tried a solution found here in stack overflow the code is like this. SELECT * FROM table WHERE ... ORDER BY CASE WHE

Can I store the SQL Server sort order in a variable?

I have the following SQL within a stored procedure. Is there a way to remove the IF statement and pass the 'ASC'/'DESC' option as a variable? I know I could do the query a number of different ways, or return a table and sort it externally etc. I woul

SQL Server sort by hour plus one hour

I have a database of events each with a Time field that needs to be sorted in ascending order from said time. The "day" for these events start at 11:00PM (events take place 24 hours / day from 11:00PM to 10:59PM). Is there a way to sort my event

Dynamic SQL Server Sorting on Multiple Columns

I have a stored procedure which uses dynamic sorting, 2 parameters determine the sorting - column: @SortIndex and sort direction: @SortDirection relevant code: ... ROW_NUMBER() OVER ( ORDER BY -- string order by CASE @SortDirection WHEN 'ASC' THEN CA

SQL Server sort column based on the same column

I have a db table containing a column display_order. The data looks like this: 2 4 7 10 12 I want to update the same db column and it should look like this: 1 2 3 4 5 Please suggest some easy SQL code.Have a look into ROW_NUMBER(), this will help you

Can SQL Server SQL_Latin1_General_CP1_CI_AS be securely converted to Latin1_General_CI_AS?

We have a legacy database with some (older) columns using "SQL_Latin1_General_CP1_CI_AS" and more recent changes have used "Latin1_General_CI_AS". This is a pain as joins need the additional COLLATE statement to work. I'd like to bring

Character set issues with Oracle Gateways, SQL Server, and Application Express

I am migrating data from a Oracle on VMS that accesses data on SQL Server using heterogeneous services (over ODBC) to Oracle on AIX accessing the SQL Server via Oracle Gateways (dg4msql). The Oracle VMS database used the WE8ISO8859P1 character set. T

Avoiding unnecessary sorting in SQL Server GROUP BY?

I have tables of data samples, with a timestamp and some data. Each table has a clustered index on the timestamp, and then a data-specific key. Data samples are not necessarily equidistant. I need to downsample the data in a particular time range in

Retrieve sorted column value in SQL Server

What i have: I have a Column ID SerialNo 1 101 2 102 3 103 4 104 5 105 6 116 7 117 8 118 9 119 10 120 These are just the 10 dummy rows. The actual table has over 100 000 rows. What I Want to get: A method or formula like any sorting techniq

Keep the SQL Server table sorted in the database

In my application I use a simple table that contains the number of times a file was accessed and the date at which it was last accessed. I'm trying to sort the saved entries so that the files with the highest number of accesses are always on top in t

Using Alternate Key to set Lookup in SSIS (KingswaySoft)

$
0
0

Alternate Keys which were introduced with CRM 2015 Update 1, gave us the ability to Upsert and Update records, by defining one or more fields as Alternate Key and use that field(s) instead of relying only on Primary Key.

Alternate Keys can also be used for setting or updating the lookup instead of GUID.

CDS\CRM Destination Component in KingswaySoft SSIS Integration Toolkit allows us the option of specifying the Alternate Key for updating the lookup field.

Suppose we have Contact entity with following Alternate Key defined in it


Using Alternate Key to set Lookup in SSIS (KingswaySoft)
Key1 on field last name Key2 on 2 fields emailaddress1 and last name

Within the CDS/CRM Destination Component Editor for the lookup field, click on ellipsis for the Text Lookup column. Here we have selected parent contact field inside Lead Entity.


Using Alternate Key to set Lookup in SSIS (KingswaySoft)

Inside Text Lookup Editor, select the option “Choose Target Field(s)”, in lookup method select Alternate Key and we will see both our alternate keys listed there.


Using Alternate Key to set Lookup in SSIS (KingswaySoft)
Key1 on field lastname
Using Alternate Key to set Lookup in SSIS (KingswaySoft)
Key 2 on emailaddress 1 and last name
Using Alternate Key to set Lookup in SSIS (KingswaySoft)

One thing I have observed that it doesn’t list the alternate key if it is defined by using more than 2 fields. Looks like it is by design.

Hope it helps..

[Video] Office Hours 2018/10/10 (With Transcriptions)

$
0
0

This week, Brent, Tara, and Richie discuss splitting large database files across multiple files, security auditing solutions, what Brent is currently teaching at the client location, SQL Server in containers, heap index maintenance, contained databases, extended events, the difference between a junior and Senior DBA, page file sizing, and more.

Here’s the video on YouTube:

You can register to attendnextweek’s Office Hours , or subscribe to our podcast to listen on the go.

If you prefer to listen to the audio:
[Video] Office Hours 2018/10/10 (With Transcriptions)

Podcast: Play in new window | Download

Enjoy the Podcast?

Don’t miss an episode, subscribe via iTunes , Stitcher or RSS .

Leave us a review in iTunes

Office Hours Webcast 2016-10-10 Should I split up a 200GB database?

Brent Ozar: Paul says, “One of the databases that I monitor is 200GB in size, but it only has one file. Should I go ahead and split that up across multiple files?” Tara, you’re the DBA on the call.

Tara Kizer: You too. I mean, it’s fairly unnecessary unless you have special disks, you know, a special layout where you can add a file and some of your objects will be on that file. So if you have some faster disks and you want to move some of your heavily read tables, then move them over to another file, adding another file group. But size of database doesn’t really mean you have to have more than one data file.

How should I audit SQL Server?

Brent Ozar: Jason says, “Does anyone have any suggestions on a solution for security auditing for SQL Server? Should I use in-house or third-party options?”

Tara Kizer: I have no idea. I feel like the clients that have auditing, there’s performance issues.

Brent Ozar: Yeah, you really what I tell people is, do you have to defend yourself in front of a lawyer and a judge? Is this going to be something where you have to legally defend yourself in court? And if you do, then that kind of guides the solution that you’re going towards because you shouldn’t have something that a DBA can turn off, something that can lose events really easily. So this kind of rules out a lot of things that you would roll yourself because you would be a suspect. You have to be able to stand in court and say there wasn’t a problem. So if you have to be legally defensible, I’m a huge fan of third-party appliances like Imperva or Guardium. These are big expensive appliances that sit in between SQL Server and the rest of the network. And they log every network packet, they track who sees what, because it’s not just a matter of who does inserts, updates, and deletes. It’s a matter of who sees whose patient’s records, for example. And there is an overhead with that. it’s not usually huge, but those things are six figures a piece. Whereas when you go roll your own, there’s usually all kinds of overhead, even if you’re using SQL Server’s auditing. And it’s not going to be legally defensible in those cases. So that’s just the first place that I would start if you have to defend yourself in front of a court. If you don’t have to, if you don’t care about anything legally defensible, go but IDERA’s SQL Compliance Manager. And I’m not saying it’s not legally defensible, but I’m saying that it’s about a thousand, two thousand bucks, you install it and then it just logs to a file. Sure, you could get around it, but at least that way, you’ve checked the box and you can go on with your life.

What is Brent teaching today?

Brent Ozar: Let’s see, Michael says, “What am I teaching and doing today at the client location?” We just finished a parameter sniffing class and I don’t actually know what we’re talking about next. I have to go look at the it’s all database developers in the room today, so I’m doing a lot of T-SQL type stuff. So between Richie and I, Richie’s dancing and laughing, like yay for database developers. But Richie is also inheriting a hot mess of SQL that I have in our current production environment…

What did Brent break this weekend?

Richie Rump: Please tell them what happened this weekend, Brent. Please, share with the class what went on this weekend.

Brent Ozar: So I used queries behind PowerBI. I used PowerBI to build a little dashboard so for SQL ConstantCare I can tell people, here’s the problem you’re having, here’s the issues behind it…

Richie Rump: And it’s great. And it’s great…

Brent Ozar: Well, no. Richie’s been hearing me complain about PowerBI. It’s the only thing we have it’s pretty cool. It’s gorgeous. It helps me get to answers quickly. But it all depends on the quality of my queries. My queries are not very good and I was pointing at a specific replica rather than pointing at a generic reader name, like an AG listener. So when we were running both production and my PowerBI queries on the same replica, I may have caused our production servers to failover multiple times. I’m trying to troubleshoot why the server’s having problems and then I’m like, oh, it turns out…

Richie Rump: Yeah, you know, because of the way we’ve architected it, it was really easy for us to recover those files and then push them out into nobody lost any data. Nobody didn’t get their messages, and that’s the great part about working in the cloud and in this type of environment; hey, something fails, we just push it right back through and, hey, it goes and it finishes. That has been a lot of fun. It’s like, oh, failed? Oh, no problem, just push it right back through.

Have you worked with SQL Server in containers?

Brent Ozar: Andy Leonard asks a serious question. He says, “Have y’all played with SQL Server in containers?” And we laugh, but oh man. The serious answer is no, we no, that’s not true. I did play with them, I just stopped.

Richie Rump: I mean, is Aurora running on container? I would assume that it is, so technically yes.

Brent Ozar: Well, it’s not SQL Server though, but…

Richie Rump: It’s not SQL Server.

Brent Ozar: It doesn’t solve a problem for us. I get that somewhere somebody has a problem that containers solve, I just haven’t seen it yet. That’s not true; continuous integration, continuous deployment, that totally makes sense, I get it. But for the rest of people, for production SQL Servers, I haven’t seen the problem that it solves yet that people went, oh, we could put this in a container and it’s cool now, let’s do it. I haven’t seen that. And we work mostly on production servers, so…

Richie Rump: Okay … and let’s qualify that. For continuous integration, yes. Continuous deployment, I’m not sure. I don’t think so because continuous deployment, what are we going to do, deploy to database again? And what happens to the data that’s currently in production? So yes, continuous integration where I need to restore to a state and all I h

SQL server queries are very slow only when first run

$
0
0

Somewhat strange problem... when I start my .NET app for the first time after rebooting my machine, the SQL Server queries are really slow... when I pause the debugger, I notice that it's hanging on getting the response from the query. This only happens when connecting to a remote SQL server (2008)... if I connect to one on my local machine, it's fine. Also, if I restart the app, it works fast, even off the remote SQL server, and subsequent runs are also fine. The only problem is when I connect to a remote SQL server for the first time after rebooting my machine. What's more, I have even noticed this same exact behavior with a 3rd party app (also .NET) that also connects to a remote SQL server.

Another piece of info... this has only started hapenning since I upgraded my machine from XP to Win7 (64 bit). Also, other developers on my team who upgraded to Win7 are seeing the same behavior (both with the app we're developing and the 3rd party .NET app).

EDIT: also copied to https://serverfault.com/questions/100141/sql-server-queries-are-really-slow-only-on-first-run due to suggestion by a commenter

Most likely you are enjoying the caching affect. When you first run a query, SQL generates an execution plan and then caches it. If you run the query again, it remembers the execution plan and in some cases you see a speed gain. So if you are testing a query, you need to clear the cache. Below is what I do.

Quote from Devx website ( DEVx Tip )

run DBCC DROPCLEANBUFFERS, which clears all data from the cache. Then run DBCC FREEPROCCACHE, which clears the stored procedure cache.

HTH

SQL Server Query Log for Failed &sol; Incorrect Queries&quest;

$
0
0

I am using SQL Server 2008 and I would like to know if there's a way to find invalid SQL queries that have been executed in a database. For example:

SELECT * FROM NonExistingTable

...where the database didn't exist or a SELECT/INSERT/UPDATE with incorrect syntax.

SQL Server doesn't keep a log of these things, so if you want to capture them you'll have to do so using a server-side trace with a filter to only capture statements with errors. It will be quite an expensive trace, and you'll get some false positives if you do things like RAISERROR WITH NOWAIT ... I guess it's easier than implementing TRY / CATCH everywhere and logging the errors yourself?

There may be ways to do it with SQL Server Audit (depending on your edition) or Extended Events but I haven't tried to do this specific thing with either...


How to Find Queries Using an Index From SQL Server Plan Cache? Interview Ques ...

$
0
0

Question:How to Find Queries Using an Index From SQL Server Plan Cache?

Answer:During the recent Comprehensive Database Performance Health Check , I had an interesting situation, where I suggested that we will drop an index for a query. I had clearly proved with the help of my script that the index is not used at all since the last SQL Server restart (in that case it was over 9 months).


How to Find Queries Using an Index From SQL Server Plan Cache?   Interview Ques ...

However, the company CEO who was the original DBA for their system was on the call and he really wanted to be sure that they are not using that index as he was the original creator of the index. Even though it was clear from the unused index script, he wanted to get the extra assurance that his index is not used at all by some other way.

At that time, I ran the following script which goes into the plan cache and checks if Index is used by any query in the recent time or not. Please note that plan cache cannot depend on 100% but it for sure provides information about the queries which are currently in the cache.

Let us see the script quickly.

-- Query to identify query using any speicific index -- Pinal Dave (https://blog.sqlauthority.com) SELECT SUBSTRING(sqltext.text, (deqs.statement_start_offset / 2) + 1, (CASE deqs.statement_end_offset WHEN -1 THEN DATALENGTH(sqltext.text) ELSE deqs.statement_end_offset END - deqs.statement_start_offset) / 2 + 1) AS sqltext, deqs.execution_count, deqs.total_logical_reads/execution_count AS avg_logical_reads, deqs.total_logical_writes/execution_count AS avg_logical_writes, deqs.total_worker_time/execution_count AS avg_cpu_time, deqs.last_elapsed_time/execution_count AS avg_elapsed_time, deqs.total_rows/execution_count AS avg_rows, deqs.creation_time, deqs.last_execution_time, CAST(query_plan AS xml) as plan_xml FROM sys.dm_exec_query_stats as deqs CROSS APPLY sys.dm_exec_text_query_plan (deqs.plan_handle, deqs.statement_start_offset, deqs.statement_end_offset) as detqp CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS sqltext WHERE detqp.query_plan like '%Name of Your Index Here%' ORDER BY deqs.last_execution_time DESC OPTION (MAXDOP 1, RECOMPILE); GO

In this query in the WHERE condition, you will have to insert your index name and it will bring out the elaborative result from the plan cache.

Let me know if you find this query helpful or if you use any other query. I will be happy to share the query on this blog with the due credit to you.

Reference: Pinal Dave ( https://blog.SQLAuthority.com )

Stop SSIS Package Execution in SSISDB

$
0
0

Suppose a package has been running for quite a long time or has been mistakenly run and we would like to stop its execution.

To do so

Right Click on SSISDB Select All Execution Reports
Stop SSIS Package Execution in SSISDB
Filter it to see all the Running packages
Stop SSIS Package Execution in SSISDB
We can see our all the running packages there.
Stop SSIS Package Execution in SSISDB
Now to stop any specific package, right click SSISDB and select Active Operations
Stop SSIS Package Execution in SSISDB
From the Active Operation window, select the running job and click on Stop button to stop its execution.
Stop SSIS Package Execution in SSISDB
Another option is to use the stored procedure à get the operation id of the running package and execute the stored procedure
Stop SSIS Package Execution in SSISDB
Exec catalog.stop_operation @operation_id =199915
Stop SSIS Package Execution in SSISDB

Hope it helps..

IN SQL Server数据库

$
0
0

在我的数据库中我有这个char .我想查询他们

Select * from Sometable where somecolumn like '%%'

这让我没有结果.

我认为这是ANSI编码

使用N如下

where col like N'%%'

why do you think ,you need N prefix :

Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.

感谢马丁史密斯,早些时候我只测试了一个字符,它的工作,但正如马丁指出,它返回所有字符..

以下查询工作和返回只有意图

select * from #demo where id like N'%%' COLLATE Latin1_General_100_BIN

演示:

create table #demo ( id nvarchar(max) ) insert into #demo values (N''), ( N'')

要了解更多关于unicode,请看下面的链接

http://kunststube.net/encoding/

https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/

http://stackoverflow.com/questions/43474596/in-sql-server-database

Sql server tables

$
0
0
Extend the Excel changes to the sql server table

So I'm in Excel and I connected to my SQL Server Table. I updated a couple columns in Excel. column1,column2 updated1, updated2 updated3, updated3 Is there a way in the VBA Immediate Window to run an update statement to propagate the changes back to

Insert float value in SQL Server table

I have a float value that I'm trying to insert into a SQL Server table with this code: SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand command = new SqlCommand("AddNewValue", connection); command.CommandType = CommandT

How do I lock a SQL Server table of other processes until my transaction unlocks it?

I need to make an update to a sql server table to a busy table. But, I want to make sure no other process is able to update or select for update on that table until I am done making my changes. However, I don't want all other request to fail, I just

Removing duplicates from the SQL Server table

I have a situation in my SQL Server table where I have numerous duplicates of records like following: SID Username InQueue ------------------------------------------------------------------ 162 peeeer2 492 2017-01-18 12:20:21.820 0 354 2791 peeeer2 4

SQL Server Table to FoxPro Table Issues

I am trying to get some SQL server tables into FoxPro dbf. I successfully managed to bcp export data such that when I use FoxPro Import Wizard, the data imports correctly. But when I try to do it at the command line, I can at best import the first fe

How to Select Separately Delimited Texts and Completely Column the SQL Server Table

I have a SQL Server table with a column p_author containing semi-colon (;) delimited text value. I used this query to split and select the respective id's from another table but it only splits once whereas I need all the value id's after splitting ei

Select n random rows from the SQL Server table

I've got a SQL Server table with about 50,000 rows in it. I want to select about 5,000 of those rows at random. I've thought of a complicated way, creating a temp table with a "random number" column, copying my table into that, looping through t

Problem with the MaskMoney jQuery plugin and Decimal field type (SQL SERVER table)

I am using a ASP.NET MVC framework, jQuery library (1.3.2), linq to sql, sql server 2005. I have a problem with jQuery MaskMoney plugin, to save data in sql server table. I created a table, where this table have a field "valueProducts" type Deci

The SQL Server table is sorted by default

I have simple SSIS package where I import data from flat file into SQL Server table (SQL Server 005). File contains 70k rows and table has no primary key. Importing is sucessful but when I open SQL Server table the order of rows is different from the

Notify a C ++ application when a change to a SQL Server table is performed

I've posted this before but haven't obtained a suitable answer that fits my requirements. I'm looking for a technology to notify a C++ application when a change to a SQL Server table is made. Our middle-tier is C++ and we're not looking to move onto

How to find a non-English language and its data in a sql server table

I have a Sql Server table loaded with data from multiple countries. Say Japanese, Thai, Urdu, Portuguese, Spanish and many more which i didn't identify. How to identify the language and its relevant data from that table ? sample: colid | colname 1 |

Increase the performance of the SSIS packet unload data from the Excel to SQL Server table

I have created an SSIS Package that dumps data from Excel file to SQL Server table. The package is simply having 2 tasks: Script Component Task & OLE DB Destination Please find below the code i have added in the script task: /* Microsoft SQL Server I

How to fill a SQL Server table with user information from Active Directory?

Using VB.NET in an SSIS package, how do I populate an SQL Server table with the users in multiple Active Directory domains (in the same forest)?Script Component (VB.NET) with System.DirectoryServices Here is a sample logic that import Active Director

Conversion of a value in VB.Net into bit when inserted into the Sql Server table

For X in the VB.Net code below, I need to pass in a bit value. what is the equivalent of a bit in VB.Net to make the conversion and insertion occur into the Sql Server table The parmList_chg is of type String because most of the values being passed i

How to query a SQL Server table with an interval of months

I have a SQL Server table that have data store in this format, I want to extract data from March 2014 to Feburary 2015, how can I achieve this, please? ID Months Years fd1 fd2 fd3 fd4 ------------------------------------------- 1 january 2014 1 2 3 8

创建存储过程如果不存在于sql server中

$
0
0

Oracle确实“创建或替换”语句. Sql server似乎没有 如果您从企业管理器脚本化,而是建议“删除和创建”.在您对存储过程进行授权的任何情况下,丢弃和创建都是不可取的,因为它会抛出数据库管理团队所做的任何授权.您真的需要“创建或替换”来帮助开发人员和管理员之间的分离.

我最近一直在做的是这样的:

use [myDatabase] go create procedure myProcedure as begin print 'placeholder' end go alter procedure myProcedure as begin -- real sproc code here end go

这样做我想要的如果程序不存在,创建它然后更改正确的代码.如果程序确实存在,则create将失败,alter更新代码.

它会为管理员创建一个不同的问题,因为如果存储过程已经存在,则create会引发误导性的错误.当然,当你想要的结果发生时,你不应该看到红色的错误文本.

有人有办法压制红色文本吗?我尝试过的一切导致“CREATE / ALTER PROCEDURE必须是查询批处理中的第一个语句”错误.

这将工作并保持权限不变: use [myDatabase] go if object_id('dbo.myProcedure', 'p') is null exec ('create procedure myProcedure as select 1') go alter procedure myProcedure as SET NOCOUNT ON -- real sproc code here. you don't really need BEGIN-END go

http://stackoverflow.com/questions/13866004/create-stored-procedure-if-doesnt-exist-in-sql-server

Running R scripts within in-database SQL Server Machine Learning

$
0
0

Having all the R functions, all libraries and any kind of definitions (URL, links, working directories, environments, memory, etc) in one file is nothing new, but sometimes a lifesaver.

Using R function source is the function to achieve this. Storing all definitions, functions, classes on one place can help enterprises achieve faster installation and safer environment usage.

So the idea is simple. Stack all the needed functions, classes, libraries and configurations you want to use in a specific environment and save it in a R file. Create a file with all the needed setups of libraries and functions, as seen below. We will call this file as util.R.


Running R scripts within in-database SQL Server Machine Learning

And the rest is just to refer to this file in any R environment or R script with simple call using source.

setwd("C:\\MyFiles\\R_setup") source("util.R")

Any new R script will have now all the functions and libraries included by default. Testing it with the simple function call is straightforward:

x <- c(1,2,3,4,5) y <- c(2,3,4,5,6) mysummary(x) sss(x,y)

and the result is, as expected


Running R scripts within in-database SQL Server Machine Learning

In my previous blog posts, I have mentioned also calling R scripts from SQL Server tables, external files and others. Using R source function is another way to pull in predetermined and preinstalled functions also in SQL Server in-database machine learning service (or machine learning server), that will make setting up same environments on client machines in enterprise environment much easier and faster. With just one update of the file, all client machines can read same definitions.

Using same util.R file, I have placed the file in the location where SQL Server R and workers will have access granted. By using sp_execute_external_script and external R file, this can be simplified:

USE AdventureWorks; /* SELECT n FROM (VALUES(1),(2),(3),(4)) AS tbl(n) */ exec sp_execute_external_script @language = N'R' ,@script = N' #have access to this folder for SQL SERVER /R workers source("C:\\MyFiles\\R_setup\\util.R") x <- InputDataSet x <- as.numeric(x) out <- addOne(x) OutputDataSet <- data.frame(as.numeric(out))' ,@input_data_1 = N'SELECT n FROM (VALUES(1)) AS tbl(n)' WITH RESULT SETS (( FunctionResult VARCHAR(100) ))

Result is the same as if ran from and other R environment, but this time, I am running it from SQL Server.


Running R scripts within in-database SQL Server Machine Learning

There are couple of thoughts to point out, though:

1) have the util.R file or any external R file validated (!)

2) all the libraries, that util.R file is referring to, must be installed. If not, the external file will not be loaded. This can simply be achieved by using:

instead of just referencing the package by using library(AGD).

3) there should be more IF statements used regarding the R core engine and to check the availability and compatibility of new packages.

4) not all packages can be used with Microsoft R engine and the current R version.

Enjoy R and SQL Server and happy coding.

存储过程 重新编译后,存储过程运行得很快

$
0
0

我在SQL Server 2008 R2上存储过程有一个非常奇怪的问题.有时,每个月大约一次,我有一个程序变得非常慢,大约需要6秒运行而不是几毫秒.但是,如果我只是重新编译它,而不改变任何东西,它会再次运行.

所有存储过程都不会发生,只有一个(服务器上有几百个).

我的猜测是当sp被编译时,它被缓存,这个缓存在每次调用它时被重用,并且这个缓存版本由于某种原因而被破坏.

我希望也许有些人已经面临这种问题,或者至少可以指出我在正确的方向,像SQL Server或IIS的配置可能会影响存储过程缓存?

以下是代码:

USE [MyBaseName] GO /****** Object: StoredProcedure [dbo].[Publication_getByCriteria] Script Date: 05/29/2013 12:11:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[Publication_getByCriteria] @id_sousTheme As int = null, @id_theme As int = null, @nbPubli As int = 1000000, @bActuSite As bit = null, @bActuPerso As bit = null, @bActuNewsletter As bit = null, @bActuChronique As bit = null, @bActuVideo As bit = null, @bActuVideoBuzz As bit = null, @bActuOpportunite As bit = null, @id_contact As int = null, @bOnlyPublished As bit = 0, @bOnlyForHomePage as bit = 0, @id_contactForTheme As int = null, @id_newsletter As int = null, @ID_ActuChronique As int = null, @sMotClef As varchar(500) = null, @sMotClefForFullText as varchar(500) = '""', @dtPublication As datetime = null, @bParlonsFinance As bit = null, @bPartenaires as bit = null, @bUne As bit = null, @bEditoParlonsFinance As bit = null, @bEditoQuestionFonds as bit = null, @dtDebPublication As datetime = null, @dtFinPublication As datetime = null, @bOnlyActuWithDroitReponse As bit = 0, @bActuDroitReponse As bit = null AS BEGIN SET NOCOUNT ON; DECLARE @dtNow As datetime SET @dtNow = GETDATE() SELECT TOP (@nbPubli) p.id_publication, p.sTitre, p.sTexte, p.sTexteHTML, p.dtPublication, p.id_linkedDroitReponse, si.id_actusite, pe.id_actuPerso, ne.id_actuNewsletter, ac.id_actuChronique, av.id_actuVideo, ap.id_actuOpportunite, ad.id_actuDroitReponse, c.ID_Contact, c.sPhotoCarre, NULL As sTypePubli, n.id_newsletter, dbo.Publication_get1Theme(p.id_publication) As theme, CAST(CASE WHEN ad.id_actuDroitReponse IS NULL THEN 0 ELSE 1 END As bit) As bIsDroitReponse, coalesce(Personne.sNom, Societe.sNom) as sNom, Personne.sPrenom FROM Publication p LEFT OUTER JOIN ActuSite si ON p.id_publication = si.id_publication LEFT OUTER JOIN ActuPerso pe ON p.id_publication = pe.id_publication LEFT OUTER JOIN ActuNewsletter ne ON p.id_publication = ne.id_publication LEFT OUTER JOIN ActuChronique ac ON p.id_publication = ac.id_publication LEFT OUTER JOIN ActuVideo av ON p.id_publication = av.id_publication LEFT OUTER JOIN ActuOpportunite ap ON p.id_publication = ap.id_publication LEFT OUTER JOIN ActuDroitReponse ad ON p.id_publication = ad.id_publication LEFT OUTER JOIN Contact c ON p.id_contact = c.ID_Contact LEFT OUTER JOIN Personne ON Personne.id_contact = c.id_contact LEFT OUTER JOIN Societe ON Societe.id_contact = c.id_contact LEFT OUTER JOIN Newsletter n ON ne.id_actuNewsletter = n.id_actuNewsletter WHERE p.bSupp = 0 AND (@bOnlyPublished = 0 Or (@bOnlyPublished = 1 AND p.dtPublication IS NOT NULL AND p.dtPublication < @dtNow)) AND (@id_sousTheme IS NULL Or p.id_publication IN(SELECT id_publication FROM PubliSousTheme WHERE id_soustheme = @id_sousTheme)) AND (@id_theme IS NULL Or p.id_publication IN(SELECT id_publication FROM PubliTheme WHERE id_theme = @id_theme)) AND ((@bActuSite = 1 AND si.id_actusite IS NOT NULL) OR (@bActuPerso = 1 AND pe.id_actuPerso IS NOT NULL) OR (@bActuNewsletter = 1 AND ne.id_actuNewsletter IS NOT NULL) OR (@bActuChronique = 1 AND ac.id_actuChronique IS NOT NULL) OR (@bActuVideo = 1 AND av.id_actuVideo IS NOT NULL) OR (@bActuVideoBuzz = 1 AND av.id_actuVideo IS NOT NULL and coalesce(av.sBuzz, '') <> '' ) OR (@bActuOpportunite = 1 AND ap.id_actuOpportunite IS NOT NULL) OR (@bActuDroitReponse = 1 AND ad.id_actuDroitReponse IS NOT NULL)) AND (@id_contact IS NULL Or p.id_contact = @id_contact) AND (@id_contactForTheme IS NULL Or (p.id_publication IN(SELECT id_publication FROM PubliSousTheme WHERE id_soustheme IN(SELECT id_soustheme FROM ContactSousTheme WHERE id_contact = @id_contactForTheme))) Or (p.id_publication IN(SELECT id_publication FROM PubliTheme WHERE id_theme IN(SELECT id_theme FROM ContactTheme WHERE id_contact = @id_contactForTheme))) ) AND (@ID_ActuChronique is NULL or id_actuChronique = @ID_ActuChronique) AND (@id_newsletter IS NULL Or p.id_publication IN(SELECT id_publication FROM ListActuNewsletter WHERE id_newsletter = @id_newsletter)) AND (@sMotClef IS NULL or contains((p.sTexte, p.sTitre), @sMotClefForFullText) Or Personne.sNom LIKE '%' + @sMotClef + '%' COLLATE Latin1_General_CI_AI Or Personne.sPrenom LIKE '%' + @sMotClef + '%' COLLATE Latin1_General_CI_AI Or Societe.sNom LIKE '%' + @sMotClef + '%' COLLATE Latin1_General_CI_AI ) AND (@dtPublication IS NULL Or p.dtPublication >= @dtPublication) AND ( @bParlonsFinance IS NULL Or (@bParlonsFinance = 0 AND p.id_publication NOT IN(SELECT id_publication FROM PubliTheme WHERE id_theme IN(SELECT id_theme FROM Theme WHERE bParlonsFinance = 1))) Or (@bParlonsFinance = 1 AND p.id_publication IN(SELECT id_publication FROM PubliTheme WHERE id_theme IN(SELECT id_theme FROM Theme WHERE bParlonsFinance = 1)))) AND ( @bPartenaires IS NULL Or (@bPartenaires = 0 AND p.id_publication NOT IN(SELECT id_publication FROM PubliTheme WHERE id_theme IN(SELECT id_theme FROM Theme WHERE 0 = 1))) Or (@bPartenaires = 1 AND p.id_publication IN(SELECT id_publication FROM PubliTheme WHERE id_theme IN(SELECT id_theme FROM Theme WHERE 0 = 1)))) AND ( @bUne IS NULL Or p.bUne = @bUne) AND (@bEditoParlonsFinance IS NULL Or p.bEditoParlonsFinance = @bEditoParlonsFinance) AND (@bEditoQuestionFonds IS NULL Or p.bEditoQuestionFonds = @bEditoQuestionFonds) AND (@dtDebPublication IS NULL Or p.dtPublication >= @dtDebPublication) AND (@dtFinPublication IS NULL Or p.dtPublication <= @dtFinPublication) AND (@bOnlyActuWithDroitReponse = 0 Or (@bOnlyActuWithDroitReponse = 1 AND p.id_linkedDroitReponse IS NOT NULL)) and (@bOnlyForHomePage = 0 or (@bOnlyForHomePage = 1 and ac.bHomePage = 1)) ORDER BY coalesce(p.dtPublication, p.dtCreate) DESC, p.id_publication DESC END

当您首次编译存储过程时,其执行计划将被缓存.

如果sproc具有参数,其定义可以显着更改包含的查询的执行计划(例如,索引扫描vs查询),则存储过程的缓存计划可能对所有参数定义最有效.

避免这种情况的一种方法是将RECOMPILE子句包含在CREATE PROCEDURE语句中.

例:

CREATE PROCEDURE dbo.mySpro @myParam WITH RECOMPILE AS BEGIN -- INSERT WORKLOAD HERE END GO

通过这样做,每当调用该过程时,将会生成一个新的计划.如果重新编译时间使用错误的缓存计划时间损失,这是值得使用WITH RECOMPILE.在您的情况下,它还会节省您每次发现其执行缓慢时手动重新编译此过程所需的时间/计划.

http://stackoverflow.com/questions/16811679/stored-procedure-runs-fast-after-recompile


sql-server 如何释放空闲SQL Server数据库使用的内存?

$
0
0

我们有一个SQL Server与几百个数据库.该服务器上的许多数据库每周仅使用几次.我们有一个问题,SQL Server有时变得非常慢.当发生这种情况时,内存使用率指示为99%,并发生分页.为了解决这个问题,我们每天晚上安排重新启动SQL服务,以卸载所有这些数据库并释放内存.

释放空闲数据库使用的内存而不使SQL Server失效的正确方法是什么?我们希望自动卸载在过去30分钟内未被访问的任何数据库.

注意:我正在寻找一个适用于SQL 2005的解决方案.但是,如果SQL 2008中有一个功能,我想知道这一点.

SQL Server将自动释放所有可以释放的内存,并避免分页.如果遇到分页,则99%的内存正在使用中,无法释放.您需要调查内存是如何使用的,可能是外部组件(如sp_oa_xxx)创建的对象或分布式查询.首先调查内存消费者,查看 sys.dm_os_memory_clerks 并阅读 How to use the DBCC MEMORYSTATUS command to monitor memory usage on SQL Server 2005

.

作为附注,您已经有自动关闭不使用的数据库的方法: alter database <dbname> set auto_close on :

AUTO_CLOSE : When set to ON, the

database is shut down cleanly and its

resources are freed after the last

user exits. The database automatically

reopens when a user tries to use the

database again.

如果您托管数百个很少使用的数据库,那么AUTO_CLOSE正是您要查找的.

http://stackoverflow.com/questions/5795033/how-to-free-up-memory-used-by-idle-sql-server-databases

Add new article to existing publication for SQL Server Transactional Replication

$
0
0

By: Nisarg Upadhyay || Related Tips:More >Replication

Problem

SQL Server Replication is a great tool to transfer data between different instances of SQL Server as well as other non-SQL Server databases. When setting up replication, you never know what new database changes will occur, so there is often the need to add additional objects to the publication. In this tip we will look at how to add a new article (database object) to an existing SQL Server publication.

Solution

In this tip, I am going to demonstrate how to add an article (database object) to an existing replication publication.

I have set up transactional replication as follows:

Replication Type: Transactional Replication Publisher database: AdventureWorks2014 on instance TTI412-VM\SQL2017 Subscriber database: Customer_DB on instance TTI412-VM

I have a publication named Customer_Publication on the publisher database. The publication has one article which is a table named Customers. Replication has already been setup with the subscriber, so on the subscriber instance the same table, Customers , exists in the database Customer_DB.

According to the concept of transactional replication, row and data changes made on the Customers table at the publisher, replicate to the subscriber table Customers.

The following image illustrates the set-up:


Add new article to existing publication for SQL Server Transactional Replication

To review the publisher information open SQL Server Management Studio, expand the SQL Instance, expand Replication then expand Local Publications , as shown below. We can see we have a publication called Customer_Publication .


Add new article to existing publication for SQL Server Transactional Replication

To review the subscriber information open SQL Server Management Studio, expand the SQL Instance, expand Replication and expand Local Subscriptions as shown below:


Add new article to existing publication for SQL Server Transactional Replication

Alternatively, you can view the replication settings by executing T-SQL queries. You can review the scripts inthis article. The following output has been generated after executing the T-SQL scripts.

The Output of Publisher Server:


Add new article to existing publication for SQL Server Transactional Replication

The Output of Subscriber Server:


Add new article to existing publication for SQL Server Transactional Replication
Add Article in Replication using SQL Server Management Studio

You can add articles to an existing publication from the Properties window on the publisher server. To open the Properties window, in SQL Server Management Studio, expand Replication , expand Local Publications and right click on the Customer_Publication and select Properties as shown below.


Add new article to existing publication for SQL Server Transactional Replication

A publication Properties dialog box opens. In the dialog box, select the Articles page. It populates the list of objects which are already part of the publication. We can see we only have the Customers table as part of this publication.


Add new article to existing publication for SQL Server Transactional Replication

To get a list of all database objects, un-check the checkbox for Show only checked articles in the list as shown below. Then select the list of database objects to add to the existing publication. Below I have highlighted the five tables I will add to the publication. After selecting the objects, click OK to close the dialog box.


Add new article to existing publication for SQL Server Transactional Replication

After adding the articles to the existing publication, we must re-initialize the subscriptions with a new snapshot. To do that, right click on the publication and select Reinitialize All Subscriptions as shown below.


Add new article to existing publication for SQL Server Transactional Replication

A dialog box Reinitialize Subscription(s) opens. Select Use a new snapshot and then click the Mark For Reinitialization button.


Add new article to existing publication for SQL Server Transactional Replication

We must create a new snapshot after adding the new articles. The new snapshot can be generated using Replication Monitor . To open Replication Monitor, right click on the Customer_Publication and select Launch Replication Monitor as shown below.


Add new article to existing publication for SQL Server Transactional Replication
When Replication Monitor opens, click on the Agents tab. In the dropdown select Snapshot Agent from the Agent types . From the list of snapshot agents, right click on [AdventureWorks2014].[Customer_Publication] and select Start Agent as shown below.
Add new article to existing publication for SQL Server Transactional Replication

Once the Snapshot Agent runs, you can view the process in the Last Action.


Add new article to existing publication for SQL Server Transactional Replication

Alternatively, you can run the snapshot job from the View Snapshot Agent Status dialog box. To open it, expand Local publications and right click on Customer_Publication and select View Snapshot Agent Status. In the dialog box, click the Start button. You can see the status of a snapshot process in Status text box.


Add new article to existing publication for SQL Server Transactional Replication

Once the snapshot job completes, the new articles will be added to the subscribers. To verify, connect to the subscriber database and execute the following query.

use Customer_DBgo
select name,create_date,type_desc
from sys.tables
where name not like '%MS%'

Following is the output of the above query. This shows all of the tables that now exist in the Customer_DB database on the subscriber.


Add new article to existing publication for SQL Server Transactional Replication

Let's check the status of the publisher and subscriber. To do that, re-execute the queries we used above.

The Output of Publisher Server:


Add new article to existing publication for SQL Server Transactional Replication
Summary

In this tip, I have explained how to add an article to existing transactional replication publication.

Next Steps Read more tips about replicationhere. Read interview questions related to replicationhere.

Last Update: 2018-10-15


Add new article to existing publication for SQL Server Transactional Replication
Add new article to existing publication for SQL Server Transactional Replication
About the author
Add new article to existing publication for SQL Server Transactional Replication
Nisarg Upadhyay

SQL Server Parallelism Overview

$
0
0

By: Pablo Echeverria || Related Tips:More > Database Configurations

Problem

In this tip we’re going describe what parallelism is, the benefits, and how to take advantage of parallelism in SQL Server with some examples.

Solution

To define parallelism, there are some basic concepts we need to define:

Throughput : The number of tasks completed per unit of time. If you have a 2-seat car (excluding the driver) that travels at 150 MPH, and a 40-seat bus (excluding the driver) that travels at 50 MPH, the car is 3x faster but in a 150-mile trip for 3 hours the bus can deliver 10x more people than the car, or if we wait for the car to deliver 40 people it takes 13x more time than the bus. Thread : One independent path of execution through the code. The number of simultaneous active threads is up to the number of CPU’s or cores. Note that not all threads have to follow the same path, but the throughput increases if they do. Multithreading/multiprocessing : The ability to execute multiple processes simultaneously. If you have a color printer, it would be cumbersome if you need to print one color at a time, times the number of pages. Automation : If a machine can do a task, there’s no need for you to force it working in a specific way. If you have a 4-helix airplane, it's nonsense that you iterate through each spin for each one of the helixes must make. Even if it will eventually move the plane, the plane will never take off.

Parallelism can be defined as achieving better throughput by the execution of multiple threads at the same time or the automation of tasks. It is achieved by keeping the hardware busy using a lot of threads, processing multiple items per thread, and/or performing multiple operations with the same data already in memory. By keeping the hardware busy, threads spend less time waiting on a resource that is unavailable at that moment and being forced to switch context. This is better explained in the article fully utilize all bottlenecks , from where the image below is from:


SQL Server Parallelism Overview

The benefits of implementing parallelism are:

We can solve larger problems or solve more problems in less time. Usually you end up having less operations performed, or the same number of operations, but with less steps.
SQL Server Parallelism Overview
Get a return on your investment. If you’re doing serial programming on a 4-CPU computer, the other three CPU’s are consuming power and generating heat without being needed nor used. Also, the SQL Server license is per-core, so its better if you’re using the cores for which you paid a license for. Increase the computing power beyond the local resources. There’s no need to have centralized software that monitors all the servers (and consuming all network bandwidth and CPU in the run) if each server can monitor itself and send alerts when appropriate consuming its local network bandwidth and CPU. And there are algorithms that can be parallelized and distributed among multiple CPU’s and/or nodes in a network. Examples of this aremulti-server queries, multi-server jobs , machine learning server and GPU acceleration .

Note that parallelism is helpful for people in all IT levels:

For managers, it helps them decide on which software to purchase, based on its capacity and computations per cycle. For infrastructure support, it helps them recommend the best hardware based on their characteristics and configure the virtual machines and operating systems in a way that utilizes the resources correctly. For database administrators, it helps them configure the database servers in a way that performs better with the existing hardware and software that is already in place. For developers, it helps them create programs that utilize fully and correctly the existing hardware. Configuring SQL Server to run operations concurrently

For the operations performed inside the SQL Server engine, you must take a look at how to set the cost threshold for parallelism and the max degree of parallelism , as they may be preventing you from using parallelism. The default value of is 5, but Microsoft suggests changing it to 50 and then adjust as needed. Here is a calculator that can tell you the recommended value for MAXDOP, and here is an example on how to run a query in parallel in the engine.

Below are example query plans where one is run in parallel and the other is not.


SQL Server Parallelism Overview

Image source: https://placko.wordpress.com/2012/06/24/parallel-query-processing-in-sql-server/

For the number of simultaneous requests sent to SQL Server instance, please note that in T-SQL you can’t create multiple threads (which is a limitation of the language) even when SQL Server can handle multiple requests in parallel, but this is doable. Here are some examples on how to do it fromPowerShell, Reporting Services and Integration Services .

Below is an example in PowerShell where multiple tasks are run in parallel.


SQL Server Parallelism Overview
SQL Server Parallelism Overview
The Correct T-SQL to Work in Sets The general advice is to not create procedural programming, instead work in sets . Let the machine determine the “how”, which is what it has been programmed for. It may not be obvious how to rewrite a query without using cursors and loops, but this is doable. When you do procedural programming (loops, recursive c

SQLServer2008r2容灾备份还原及自动删除过期备份

$
0
0
完整备份和差异备份还原原理:

差异备份是完整备份的补充,只备份上次完整备份后更改的数据。相对于完整备份分来说,差异备份的数据量比完整数据备份小,备份的速度也比完整备份要快。因此,差异备份通常作为常用的备份方式。在还原数据时,要先还原前一次做的完整备份,然后还原最后一次所做的差异备份 ,这样才能让数据库里的数据恢复到与最后一次差异备份时的内容相同。


SQLServer2008r2容灾备份还原及自动删除过期备份

差异备份是备份完整备份后的数据变动情况。例如,在2018年10月1日早上8点进行了完整备份后,在10月2日和10月3日又分别进行了差异备份,那么在10月2日的差异备份里记录的是从10月1日到10月2日这一段时间里的数据变动情况,而在10月3日的差异备份里记录的是从10月1日到10月3日这一段时间里的数据变动情况。因此,如果要还原到10月3日的状态,只要先还原10月1日做的完整备份,再还原10月3日做的差异备份即可。

1.在目标系统上新建数据库SSE 2.还原完整备份:

选择最近的一次完整备份bak文件

数据库 -> 右键 -> 任务 -> 还原 -> 数据库


SQLServer2008r2容灾备份还原及自动删除过期备份

选择 覆盖现有数据库、不对数据库进行任何操作


SQLServer2008r2容灾备份还原及自动删除过期备份
3.还原差异备份

数据库 -> 右键 -> 任务 -> 还原 -> 文件和文件组


SQLServer2008r2容灾备份还原及自动删除过期备份
SQLServer2008r2容灾备份还原及自动删除过期备份

选择覆盖所有数据库、回滚未提交的事务确定即可完成差异还原


SQLServer2008r2容灾备份还原及自动删除过期备份
4.删除超过1个月的备份任务
SQLServer2008r2容灾备份还原及自动删除过期备份
SQLServer2008r2容灾备份还原及自动删除过期备份
SQLServer2008r2容灾备份还原及自动删除过期备份

Building Perfect Development Lab With SQL Server in Containers

$
0
0

I am heavily relying on virtualization in my home lab. I prefer to build separate SQL Server environments for different projects keeping them clean and isolated from each other. I can work with multiple SQL Server versions side by side without worrying how some components in my setup affect others.

This setup is also great for presentations and trainings I deliver. I just need to reset VM to the “starting” checkpoint and everything would be in the known and healthy state. Believe me, this saves you from a lot of pain and embarrassment when you are staying in front of people. Troubleshooting of non-working demos is not fun when you are on the stage.

There is the problem with this setup, however. It is much easier and faster to checkpoint existing VM rather than provision the new one for the new setup. In the end, unless you have self-discipline I lack, you may end up with something like shown in Figure 1 below. This is the screen shot of one of my VMs checkpoints.


Building Perfect Development Lab With SQL Server in Containers

01. Mess in VM configuration

On the surface, this setup may provide you required isolation between environments. However, there is the problem with maintainability. Consider how much efforts it would take to apply new SP or CU to all of your checkpoints. In the end, you will be in the situation when you always want to rebuild your lab and you never have time to do it properly. Thus, I decided to look at other options and evaluate Docker and SQL Server in containers when I recently had to setup the new travel laptop. I found that containers are better suited to my needs comparing to VM-based virtualization and I want to share my experience with you.

As the disclaimer, I am going to focus on the infrastructure side and maintainability and not going to discuss underlying technologies and containerization. You can read about it here if you want.

While both, containers and VMs rely on virtualization, they are conceptually different on the infrastructure side. Figure 2 illustrates the general virtual machine. .


Building Perfect Development Lab With SQL Server in Containers

02. VM and Checkpoints

Each virtual machine may consist of multiple checkpoints/snapshots, which fork it to the multiple states. However, only one state may be active at any giving point of time. Neither could you run multiple instances of the single VM simultaneously you’ll need to clone them creating separate VMs for that.

Containers, on the other hand, operate differently as shown in Figure 3. The environment consists of multiple images, which conceptually similar to checkpoints/snapshots. However, every image may have multiple containers (running or stopped), which are the active instances/environments. It is worth noting that Docker API allows you to checkpoint a container, but I see little benefits of doing that.


Building Perfect Development Lab With SQL Server in Containers

03. Images and Containers

That conceptual difference force you to use different methods to solve problems. Think about the situation when you need to setup individual development environments for each member of the team. With VM-based virtualization, you will have to create separate VMs. With containers, you need to create the base image and start multiple containers from this image.

Both approaches have pros and cons. The containers will use less disk space than VMs you will have only one image on disk. They are also easier to maintain and refresh you need to build the single image. VMs, on the other hand, provide true separation. If one developer is working on the complex feature in its own branch, there is no dependency on the shared image you can refresh other VMs keeping his one intact.

Nothing, obviously, prevents you from having multiple images achieving the same degree of separation with containers. You just need to manage them a bit differently. I, personally, believe that containers will be easier to maintain and lead to the cleaner setup in the long run.

Anyway, let’s do the setup. Mind you, I do not consider myself as the Docker expert I am sure that there are the better ways to achieve the goals. Nevertheless, I want to share several tips and tricks I did in my environment.

I am going to use SQL Server 2017 in windows containers setting everything up on Windows 10 machine (you need to have Anniversary Update Build 1607 or later) . I am sure that you can use similar approach in different OS and/or with SQL Server in linux.

As the first step, you need to install Docker. You can download it from here you can read the instruction it is straightforward. Just make sure to choose Windows Containers during setup. After Docker setup is done, you may test your setup using Hello World image as outlined here . I did not do it after all, we are about to run SQL Server anyway.

As the next step, let’s pull SQL Server 2017 Developer Windows image. Make sure that Docker is running, open PowerShell and run the following command.

docker pull microsoft/mssql-server-windows-developer:latest

It will take some time to download but then you are pretty much done you have SQL Server “installed”. The following command will display you the list of images you have installed on your machine:

docker image ls

Figure 4 shows the output you would see.


Building Perfect Development Lab With SQL Server in Containers

04. SQL Server Image

As the side note, you can remove images using one of the following commands (obviously do not run it now). In some cases, for example if you had an error when you built the image, you may end up with the images that do not have names and you’ll need to use ID to clean them up.

docker image rm <name>
docker image rm <image_id>

Now, let’s create the container with SQL Server 2017 by running the following command. This will create the container with name sqlserver .

docker run --name=sqlserver -d -p 1433:1433 -e sa_password=Pas!w0rd12345 -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer

The -p switch sets up the port mapping between local instance and the container. In our case, both ports are the same and SQL Server in container will work (and look like) as if you have SQL Server default instance installed locally. All requests host received on port 1433 would be redirected to the container. Obviously, you need to change the port mapping if you already have SQL Server installed on your box and/or if you have multiple containers running. Also, do not forget to open the port in the firewall if you accessing the image remotely.

Now, you can connect to SQL Server by using localhost or localhost,1433 as the server name and sa/Pas!w0rd12345 for the authentication.

Alternatively, you can find IP address of the container. First, let’s run the following command to see list of active containers in the system

docker container ls -all

Figure 5 shows the output as you see, we have just one running container in the system. You can use either name or container_id during container management.


Building Perfect Development Lab With SQL Server in Containers

05. Running Containers

Next, let’s run the command below to get container information.

docker inspect sqlserver
Viewing all 3160 articles
Browse latest View live