sql-server-2008-r2

FIRST aggregate function which I can use with HAVING clause

我的梦境 提交于 2019-12-12 16:08:22
问题 I have a weird requirement which I need to use inside my Stored Procedure in SQL Server 2008 R2. I need a FIRST aggregate function which returns the first element of a sequence and I will use that with HAVING clause. Let me give you an example: DECLARE @fooTable AS TABLE( ID INT, CategoryName NVARCHAR(100), Name NVARCHAR(100), MinAllow INT, Price DECIMAL(18,2) ); INSERT INTO @fooTable VALUES(1, 'Cat1', 'Product1', 2, 112.2); INSERT INTO @fooTable VALUES(2, 'Cat2', 'Product2', 4, 12.34);

Sql server rtrim not working for me, suggestions?

一曲冷凌霜 提交于 2019-12-12 15:31:36
问题 I'm a bit surprised that I can't find a quick solution to what I'm up against, seems like it'd be a common thing to deal with. I can't get rid of the trailing spaces in my select query. I'd like to get the data into a csv file. I'm happy to copy/ paste the results from SSMS "results to text" if that's easier. Either way, my query is: declare @maxDate date = (select MAX(TradeDate) from tblDailyPricingAndVol) select p.Symbol, ',' from tblDailyPricingAndVol p where p.Volume > 1000000 and p.Clse

How to check replication snapshot agent status?

最后都变了- 提交于 2019-12-12 15:15:19
问题 I'd like to check the status of the agent after I start it using this statement EXEC sp_startpublication_snapshot @publication As I want to do a next step that needs the job to be already started. 回答1: I do not believe there is a built-in replication stored procedure to check the snapshot agent status, I could be wrong. However, you could query MSsnapshot_history. Something like this should do the trick: SELECT agent_id, runstatus, start_time, time, duration, comments, delivered_transactions,

how to get the distinct records based on maximum date?

牧云@^-^@ 提交于 2019-12-12 14:43:01
问题 I'm working with Sql server 2008.i have a table contains following columns, Id, Name, Date this table contains more than one record for same id.i want to get distinct id having maximum date.how can i write sql query for this? 回答1: Use the ROW_NUMBER() function and PARTITION BY clause. Something like this: SELECT Id, Name, Date FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Date desc) AS ROWNUM FROM [MyTable] ) x WHERE ROWNUM = 1 回答2: If you need only ID column and other columns

SQL Select Command with Unicode string is not Retrieving the Expected Data

故事扮演 提交于 2019-12-12 14:25:10
问题 I am using SQL Server 2008 R2. I want the retrieve the row from table in which data is other than than English. But when I type the command it returns me nothing. SELECT * FROM PARTY WHERE NAME LIKE 'رانا عطا ربانی' ORDER BY SRNO Any suggestions, how to retrieve record like that? 回答1: Have you tried declaring the string literal as unicode? SELECT * FROM PARTY WHERE NAME LIKE N'رانا عطا ربانی' ORDER BY SRNO [This will show you all string-related columns in your database, and their collation:

EF6 stored procedure with no results

南楼画角 提交于 2019-12-12 13:53:11
问题 My environment is VS 2012, C#, EF6 and SQL Server 2008 R2. I want to execute a stored procedure to delete records with no return value using the pattern: dataContext.Database.SqlQuery<return type>("name", parameter) If there are no results returned e.g. with NO COUNT ON what should I set as the return type? Seems that it should be simple but I cannot find an answer anywhere. I tried dataContext.Database.SqlQuery("name", parameter) but that returns a compile error: The best overloaded method

syntax error when using CASE in where clause

会有一股神秘感。 提交于 2019-12-12 13:27:59
问题 A similar question has been asked here Anyway i get a syntax error i cannot figure. This is my code: declare @MyParameter integer se @MyParameter = Set At Runtime (could be -1 or any value >=1) SELECT manyfields FROM manyjoinedtables where case when @MyParameter> -1 then (FIELD1 **=** @MyParameter AND ANOTHERFIELD = Value**)** end -- note: in case @MyParameter = -1 i do not want to add where condition Anyway Management studio underlines in red the 2 chars surrounded by ** above. Why? Where is

can a SQL Server stored proc execute with higher permission than its caller?

删除回忆录丶 提交于 2019-12-12 13:03:44
问题 Our SQL Server database has a reporting feature that allows callers to read, but not write, any table, because the user (or, more precise, the connection opened by the web app that's operating on behalf of the user) has only datareader permissions on the database. We'd like to be able to write a store procedure that is a special "cleanup report" that will scrub the DB of old cached data before running another report. We'd like the same read-only user above to be able to run this stored proc.

Creating SQL table with range of months as columns and populating fields with table data

半城伤御伤魂 提交于 2019-12-12 12:42:00
问题 I need to create an SQL table or view that would generate columns for every month between a range of two dates. So a range of 01/01/2014 - 04/01/2014 would generate four columns with headers Jan 2014, Feb 2014, March 2014, April 2014. I also need it to populate the fields with data that pertain to those months from the following table, while any field with no appropriate data would be filled in with null values: ------------------------------------------------------ | Project# | Months |

Is it possible to create a dummy filestream filegroup to study mdf content only?

余生颓废 提交于 2019-12-12 12:20:14
问题 My application has a database that I create from a backup file in this way: -- create empty db CREATE DATABASE MyNewDB -- restore on the empty db from file RESTORE DATABASE MyNewDB FROM DISK = 'c:\Temp\MyNewDB.bak' WITH REPLACE, MOVE 'MyDB_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.EXPRESS2008R2\MSSQL\DATA\MyNewDB.mdf', MOVE 'MyDB_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.EXPRESS2008R2\MSSQL\DATA\MyNewDB_log.LDF', MOVE 'MyDBFS' TO 'C:\FileStreamData\MyNewDBFS'