sql-server-2008-r2

Select top n records from each category within same table

非 Y 不嫁゛ 提交于 2019-12-11 12:05:23
问题 I've a purchase detail table that has item id , purchase date , and item unit cost . I want to get an avg of an item purchase cost by selecting latest top 2 records from each item id. Item id, purchase date, unitprice 1 3/1/2012 10 1 3/11/2012 8 2 3/1/2012 10 2 3/11/2012 10 1 2/1/2012 9 3 3/1/2012 10 3 3/11/2012 1 3 3/12/2012 13 I'm using sql server 2008 r2 回答1: Try this: ;WITH CTE AS ( SELECT [Item id], [purchase date], unitprice, ROW_NUMBER() OVER(PARTITION BY [Item id] ORDER BY [purchase

SQL Server: xp_fileexist and UNC path permissions

跟風遠走 提交于 2019-12-11 12:04:22
问题 I'm attempting to run an ad hoc query in SQL Server Management Studio that uses the undocumented stored procedure xp_fileexist . I want to test for the existence of files in a subfolder under a network shared folder accessed via a UNC path. Here's a sqlservercentral post that talks about the execution context of xp_fileexist . From what I can tell I've got all the necessary permissions in place: I'm running SQL Server Management Studio from a domain account that has Full Control of the shared

SQL case when set variable

天涯浪子 提交于 2019-12-11 11:53:28
问题 I am trying to set 2 declared variable with case when blocks Here's my code: DECLARE @like bit,@dislike bit if exists ( select * ,@like = (CASE WHEN likeordislike = 1 THEN 'true' ELSE 'false' END) ,@dislike=(CASE WHEN likeordislike = 0 THEN 'true' ELSE 'false' END) from likeordislike ) But when I execute query throws errors: Msg 102, Level 15, State 1, Line 4 Incorrect syntax near '='. Everything is ok? Couldn't understand 回答1: if (select count(*) from likeordislike where user = @user and

System.Data.EntityException: The underlying provider failed on Open

喜夏-厌秋 提交于 2019-12-11 11:22:08
问题 I am building an application which has a database on the server machine and I am using this method for database access: public static string GetDefaultConnectionString() { //get my db info from ini file dbinfo DatabaseInfo = new dbinfo(); DatabaseInfo = GetDatabaseInfo(); if (DatabaseInfo.dbName == "" || DatabaseInfo.Password == "" || DatabaseInfo.Server == "" || DatabaseInfo.UserId == "") { throw new Exception("Database config file is not valid"); } else { MessageBox.Show("Db name " +

Use sp_executesql to run xp_cmdshell with variable

有些话、适合烂在心里 提交于 2019-12-11 11:01:58
问题 Trying to run xp_cmdshell with sp_executesql without success, database is SQL Server 2008R2 Here is the SQL DECLARE @sql nvarchar(max) = N'EXEC xp_cmdshell ''BCP "SELECT data FROM TableA WHERE id = @id" queryout C:\Temp\test.dat -T -N''' EXEC sp_executesql @sql, N'@id numeric(19, 0)', @id = 1234 The error is Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Must declare the scalar variable "@id". Please help, thank you! 回答1: The error is due to the @id variable reference in your

query to limit records returned by sql query based on size of data

妖精的绣舞 提交于 2019-12-11 10:48:14
问题 I have to create multiple excel sheets in an excel document to feed into another legacy system. This legacy system whose code can't be modified does not accept any sheet with data more than 10 MB in size. At the moment I have a manual process that gets all the data with a sql query, which I then dump into a temporary workbook and then break that into multiple workbooks so that each workbook is not more than 10 MB. I then collate each of the sheets from those workbooks into one big workbook

How to save image as varbinay in sql server 2008r2?

痴心易碎 提交于 2019-12-11 10:41:30
问题 I am creating a windows application in visual studio10 and sql server 2008r2.i want to save image in database and retrieve for updation.when i use image data type my query is executed correctly ,but for varbinary datatype it doesn't work. 回答1: This was from a while back, so it may need some tinkering with paths and what-not (from an old project) protected void btn_upload_file_server_Click(object sender, EventArgs e) { if (this.ddl_determinationBit.SelectedIndex != 2) { btn_upload_file_server

How to show query result columnar(in different columns) instead of row by row?

自作多情 提交于 2019-12-11 10:16:27
问题 I asked this question in a different post but i t has been changed. I have three tables: Flight table FlightId int FlightNumber varchar(10) FlightCapacity table ID int FlightIdRef int ClassIdRef int Capacity int Class Table ClassId int Name varchar(10) Class Table: ClassId Name 1 Y 2 A Flight Table FlightId Number 1 123 2 423 FlightCapacity Table Id FlightIdRef ClassIdref Capacity 1 1 1 10 2 1 2 20 3 2 2 10 this is a simple query: select Flight.FlightNumber,Class.Name+RTRIM(FlightCapacity

SQL Select XML nodes, parametrize node number

本小妞迷上赌 提交于 2019-12-11 09:48:47
问题 I'm selecting data from a XML document, however I need to loop through each child node perform some actions on them. At present I have a while exists loop around the select but don't know how to parametrize the node number. I understand the below isn't right but would appreciate it if someone could point out the best way to parametrize the node selection. Thanks. DECLARE @nodeCount varchar(1) = '1' WHILE EXISTS (SELECT table.value('(Info/Data/DataInfo/Type/node())[' + @nodeCount + ']',

Is it possible to use local table variables in a procedure inside an sql query contained in a VARCHAR variable?

浪尽此生 提交于 2019-12-11 09:36:03
问题 I have the following code: DECLARE @temp_table_1 TABLE (id int identity(0, 1), col_1 varchar(50)), @txtVar VARCHAR(MAX) INSERT INTO @temp_table_1 SELECT col_1 FROM table_1 -- This table_1 is a real table in the database. Set @txtVar = 'SELECT * FROM @temp_table_1' EXECUTE (@txtVar) The error I get is Declare variable @temp_table_1. How can I fix this? 回答1: Set @txtVar = 'SELECT * FROM myTable WHERE column_value=''' + @var1 + '''' This article will help you get a basic ideas of dynamic sql.