sql-server-2008-r2

Updating and join on multiple rows, which row's value is used?

一世执手 提交于 2019-12-02 19:01:04
Let's say I have the following statement and the inner join results in 3 rows where a.Id = b.Id, but each of the 3 rows have different b.Value's. Since only one row from tableA is being updated, which of the 3 values is used in the update? UPDATE a SET a.Value = b.Value FROM tableA AS a INNER JOIN tableB as b ON a.Id = b.Id I don't think there are rules for this case and you cannot depend on a particular outcome. If you're after a specific row, say the latest one, you can use apply , like: UPDATE a SET a.Value = b.Value FROM tableA AS a CROSS APPLY ( select top 1 * from tableB as b where b.id

Stored Procedure Variables

妖精的绣舞 提交于 2019-12-02 18:36:21
问题 I am afraid my terminology has hindered me from finding the results I am looking for on the internet. Anyhow, I have a fairly complex stored procedure that I need to pass several variables to. Instead of entering these variables several time through the stored procedure, is there a way to define a variable at the beginning of the procedure, and reference that variable throughout the procedure? For example IDNumber = 1075, 1050, 1025 Instead of having to use the where clause several time

Entity Framework Not Creating Database

孤者浪人 提交于 2019-12-02 17:29:09
Been playing around with the Code First feature of Entity Framework 4.1 using an ASP.NET MVC 3 project. However the database (SQL Server 2008 R2) does not automatically create the table mapping on application startup. Any ideas on how to make it do so? The project has only one POCO: namespace RIS.Models { public class Person { [Key] public virtual string NRIC { get; protected set; } public virtual string FirstName { get; protected set; } public virtual string MiddleName { get; protected set; } public virtual string LastName { get; protected set; } } } It also has the following database context

Sending Email in SQL Server 2008 R2

柔情痞子 提交于 2019-12-02 16:44:54
问题 I am tasked with a feature to send e-mail reminders to employees in my company if they haven't completed an attestation form via an intranet Web application. I was thinking of writing a stored procedure that gets called in a nightly database job (SQL Server 2008 R2). The proc would select employee e-mail address values and loop through them via cursor, so that for each e-mail found an e-mail is sent using msdb.dbo.sp_send_dbmail. The concern I have is that this is for a large company and tens

extended events blocked process report missing from sys.dm_xe_objects

柔情痞子 提交于 2019-12-02 16:41:21
问题 Trying to create SERVER EVENT SESSION to capture blocked_process_report & xml_deadlock_report events to a file for later analysis with the following statement; CREATE EVENT SESSION [blocked_process] ON SERVER ADD EVENT sqlserver.blocked_process_report( ACTION(sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.database_name)) , ADD EVENT sqlserver.xml_deadlock_report ( ACTION(sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.database_name)) ADD TARGET package0

DateTime fields from SQL Server display incorrectly in Excel

霸气de小男生 提交于 2019-12-02 16:35:57
Countless times during the day I am copying and pasting records from SQL Server Management Studio to Excel. My problem is that a DateTime value such as 8/23/2013 4:51:02 PM does not display correctly as shown in the image below even though it shows correctly in the function box. Changing the datatype to ShortDate fixes the display issues, but it is tedious to do since I have lots of date fields to format. Since Excel and SQL Server are both Microsoft products one would expect that Excel would be able to correctly show the date field. EDIT: So this appears to be a display issue with Excel. I

SQL Query - Performance Optimization

╄→гoц情女王★ 提交于 2019-12-02 16:26:46
问题 Im not so good at SQL, so I asked you guys for help on writing a query. SQL Query - Table Joining Problems I got an answer and it works! Its just noticeably slow. I hate to do it but Im really hoping someone out there wants to recommend some ways to optimize the query. I have not even attempted this on my own because I dont know enough about SQL to even begin googling. 回答1: What might help is to create indexes on the columns you're joining with. For example; CREATE INDEX name_for_index ON

SQL Server: Could not find type in the assembly

被刻印的时光 ゝ 提交于 2019-12-02 16:12:37
Assume the assembly dll: using Microsoft.SqlServer.Server; using System.Data.SqlClient; using System.Data.SqlTypes; using System; using System.Text; namespace CLRFunctions { public class T { [SqlFunction(DataAccess = DataAccessKind.Read)] public static String NormalizeString(String s, String normalizationForm) { NormalizationForm form = NormalizationForm.FormC; if (String.Equals(f, "FormD", StringComparison.OrdinalIgnoreCase)) form = NormalizationForm.FormD; return = s.Normalize(form); } } } Note : Target the assembly to .NET 3.5 as SQL Server doesn't support .NET 4.0 Copy the assembly to a

Search of table names

谁说胖子不能爱 提交于 2019-12-02 16:08:44
I use the following to search for strings in my stored procedures: use DBname SELECT Name FROM sys.procedures WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%xxx%' Is it easy to amend the above so that it searches Table names in a specific db "DBname" ? I'm using this and works fine SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%%' select name from DBname.sys.tables where name like '%xxx%' and is_ms_shipped = 0; -- << comment out if you really want to see them I am assuming you want to pass the database name as a parameter and not just run: SELECT * FROM DBName.sys.tables WHERE Name

Pivot in SQL 2008 R2

不问归期 提交于 2019-12-02 14:23:24
I have table like this; Date PlacementName campaignID Impressions Clicks TotalConversions Activity 01/01/2014 USA 100 5000 500 50 Mobile Book 01/02/2014 U.K 101 7000 250 30 Mobile Book 01/01/2014 USA 100 9000 800 40 Mobile TV 01/02/2014 U.K 101 6000 300 10 Mobile TV I want to pivot table for 15-20 Activity from the real table because this is just example table. I want my table look like below; Date PlacementName CampaignID Impressions Clicks Mobile Book Mobile TV 01/01/2014 USA 100 5000 500 50 NULL 01/01/2014 U.K 100 9000 800 NULL 40 01/02/2014 USA 101 7000 250 30 NULL 01/02/2014 U.K 101 6000