sql-server-2008-r2

“Merge” style operation with literal values?

社会主义新天地 提交于 2019-12-03 02:33:35
I have a table containing a student-grade relationship: Student Grade StartDate EndDate 1 1 09/01/2009 NULL 2 2 09/01/2010 NULL 2 1 09/01/2009 06/15/2010 I am trying to write a stored procedure that takes Student , Grade , and StartDate , and I would like it to check to make sure these values are not duplicates insert the record if it's not a duplicate if there is an existing student record, and it has a EndDate = NULL , then update that record with the StartDate of the new record. For instance, if I call the procedure and pass in 1 , 2 , 09/01/2010 , I'd like to end up with: Student Grade

How Database stores data internally in B-Tree/B+Tree

ぐ巨炮叔叔 提交于 2019-12-03 00:29:15
My question is that How database stores data and how it performs query internally. Suppose we have following fields in our table: ID Name Age Weight Manager and we query select * from Table1 where age>50 and weight<100 I am just curious that how it perform query internally. What will the Node of B-Tre/B+Tree contains in this example? The example you have chosen is one of the few cases where a single Tree can't do the job (two independent ranges). However, the first chapter of my work-in-progress e-Book explains the inner workings of B-Tree indexes: http://use-the-index-luke.com/anatomy/ EDIT

Hiding databases for a login on Microsoft Sql Server 2008R2 and above [closed]

给你一囗甜甜゛ 提交于 2019-12-03 00:16:04
Please can anyone assist with hiding the available databases on sql server 2008R2 or newer versions. I have a new login user that I mapped to a specific database. When logging in with the specific login user I can see all the databases on the server, although I cannot access them except for the one I mapped to the login. This is 100% but my problem is that I do not want the login to even see that those other databases are available. How do I prevent those other databases that are not mapped to the login from displaying? USE master; GO DENY VIEW ANY DATABASE TO [newlogin]; GO USE yourDB; GO

Stored Procedure runs fast after recompile

妖精的绣舞 提交于 2019-12-02 23:01:46
I have a very weird issue with a stored procedure on SQL Server 2008 R2. Sometimes, about once every month, I have one procedure that becomes very slow, takes about 6sec to run instead of a few milliseconds. But if I simply recompile it, without changing anything, it runs fast again. It does not happen on all stored procedure, only one (there are a few hundreds on the server). My guess is when the sp is compiled, it is cached and this cache is reused every time I call it, and this cached version gets corrupted for some reason. I hoped maybe some people already faced this kind of issue, or

Entity Framework 4 Stored Procedure Call Timing Out

跟風遠走 提交于 2019-12-02 22:53:11
I have a stored procedure imported into EF4, and when I call it with certain parameters after 30 seconds it throws a time out error. In SQL Server profiler I can see the stored procedure call with the proper parameters taking just over 30 seconds, which is the timeout on my application. HOWEVER when I execute the same SQL sent to the profiler in Query Analyzer it executes sub-second. What could cause this discrepancy between being called from EF and being called from SQL Server Management Studio? Full stack trace of .NET error bellow. [SqlException (0x80131904): Timeout expired. The timeout

Using the “With Clause” SQL Server 2008

会有一股神秘感。 提交于 2019-12-02 22:21:42
Can someone show me a sample SQL server script that I can look at that uses the "With Clause" ? I am trying to use this clause to iterate through 200 databases that contain the same table that I am trying to run a query on. I am trying to avoid using a cursor because the query time takes too long as well as using a while a loop. Can someone advise me as to what I can do. Thank you. Just a poke, but here's another way to write FizzBuzz :) 100 rows is enough to show the WITH statement, I reckon. ;WITH t100 AS ( SELECT n=number FROM master..spt_values WHERE type='P' and number between 1 and 100 )

SQL to remove rows with duplicated value while keeping one

风格不统一 提交于 2019-12-02 22:09:22
Say I have this table id | data | value ----------------- 1 | a | A 2 | a | A 3 | a | A 4 | a | B 5 | b | C 6 | c | A 7 | c | C 8 | c | C I want to remove those rows with duplicated value for each data while keeping the one with the min id, e.g. the result will be id | data | value ----------------- 1 | a | A 4 | a | B 5 | b | C 6 | c | A 7 | c | C I know a way to do it is to do a union like: SELECT 1 [id], 'a' [data], 'A' [value] INTO #test UNION SELECT 2, 'a', 'A' UNION SELECT 3, 'a', 'A' UNION SELECT 4, 'a', 'B' UNION SELECT 5, 'b', 'C' UNION SELECT 6, 'c', 'A' UNION SELECT 7, 'c', 'C'

Want to create a script to export Data and tables and views to a sql script

六眼飞鱼酱① 提交于 2019-12-02 22:02:16
Want to create a script to export Data and tables and views to a sql script. I have SQL Server 2008 r2. So far I've only been able to automatically generate an sqlscript for all tables and views. But the data wasn't included. OR is there any easier way to export data, tables, views, from one SQL Server to my ISP's SQL server? Regards Tea If for some reason a backup/restore won't work for you, SSMS' Generate Scripts tool includes an advanced scripting option to include data: Here are some options to think over (prioritised in terms of what I would recommend):- A simple backup and restore will

What is the syntax meaning of RAISERROR()

我怕爱的太早我们不能终老 提交于 2019-12-02 21:41:16
I just created a Instead After Trigger whose syntax is given below: Create trigger tgrInsteadTrigger on copytableto Instead of Insert as Declare @store_name varchar(30); declare @sales int; declare @date datetime; select @store_name = i.store_name from inserted i select @sales = i.sales from inserted i select @date = i.Date from inserted i begin if (@sales > 1000) begin RAISERROR('Cannot Insert where salary > 1000',16,1); ROLLBACK; end else begin insert into copytablefrom(store_name, sales, date) values (@store_name, @sales, @date); Print 'Instead After Trigger Executed'; end End In the above

How to list role members in SQL Server 2008 R2

不问归期 提交于 2019-12-02 21:38:46
I'm using the following T-SQL to obtain role members from my SQL Server 2008 R2 database: select rp.name as database_role, mp.name as database_user from sys.database_role_members drm join sys.database_principals rp on (drm.role_principal_id = rp.principal_id) join sys.database_principals mp on (drm.member_principal_id = mp.principal_id) order by rp.name When I examine the output I notice that the only role members listed for db_datareader are db roles - no user members of db_datareader are listed in the query. Why is that? How can I also list the user members of my db roles? I guess I should