sql-server-2008-r2

Can the script for median be generalised and put into a function type structure

拟墨画扇 提交于 2019-12-24 20:33:07
问题 We are using the following script to calculate the median: SELECT [Period] = 'amountPeriodA', [Median] = AVG(x.amountPeriodA) INTO #mediantable FROM ( SELECT r.customer, r.amountPeriodA, [RowASC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodA ASC, customer ASC), [RowDESC] = ROW_NUMBER() OVER(ORDER BY r.amountPeriodA DESC, customer DESC) FROM #MyExample r ) x WHERE RowASC IN (RowDESC, ROWDESC-1, ROWDESC+1) Is it possible to generalise a script like this and then code it into the server so that

SQL Server stored procedure expects parameter even though the parameter is provided

吃可爱长大的小学妹 提交于 2019-12-24 14:26:14
问题 I am using MS Visual Studio 2010 and SQL Server 2008 R2. In C# app I am trying to use a stored procedure but came accross a strange error. Here is table definition: create table bed_allotment ( bill_id bigint, bed_category varchar(50), for_days int ) Stored procedure: create procedure AddBed ( @bill_id bigint, @bed_category varchar(50), @for_days int ) as begin insert into bed_allotment values (@bill_id, @bed_category, @for_days) end Code on button click: private void button1_Click(object

Query optimization - using conditions on JOIN instead of with WHERE clause

微笑、不失礼 提交于 2019-12-24 14:07:58
问题 Inside an SP I need to find out the Id's of some clients of the first account whose Code matches any of the second account's clients. I wrote the following query that works - SELECT DISTINCT cil.Id FROM ClientIdList AS cil INNER JOIN Client AS c1 ON cil.Id = c1.Id INNER JOIN Client AS c2 ON c1.Code = c2.Code WHERE c2.AccountId = 2 ORDER BY cil.Id Here ClientIdList is a single-column table-type variable which holds the Ids of the selected clients from the first account (and I need to use this

Execute stored proc fails with GRANT EXECUTE because of table permissions

混江龙づ霸主 提交于 2019-12-24 14:04:28
问题 I have created a stored proc on schema X that does a select across 10+ tables that are in schema X and Y. I created a database role DBRole and added a new AD Group Login to it. I thought all I needed to do was grant execute on x.MyStoredProc to DBRole, but I'm getting errors because of select permission.. Stored Procedure MYSCHEMA.MyStoredProc failed: The SELECT permission was denied on the object 'myTable', database 'Db', schema 'dbo'. I wondered if it was because the tables its failing on

Group result by matching two columns in SQL Server

若如初见. 提交于 2019-12-24 13:42:24
问题 I am using SQL Server 2008 R2. I have a table called Messages where I store user messages each user send to other users. The table structure is like below. +--------+----------+-----------------+------------+ | Sender | Receiver | Message | Date | +--------+----------+-----------------+------------+ | John | Dennis | How are you | 2015-06-06 | | John | Dennis | Hi | 2015-06-05 | | Tom | John | How much is it? | 2015-06-04 | | Tom | John | Did you buy it? | 2015-06-03 | | Robin | Tom | Hey man

Reference a column and update it in the same statement

我是研究僧i 提交于 2019-12-24 13:28:38
问题 I have a table that I need to move a value from one column to another then set a new value in the original column. declare @foo table (A int, B int); insert into @Foo select 1, 0; update @Foo set B = A, A = 2; After the update does B always contain 1 or is this non deterministic behavior and it will sometimes have a value of 2 due to A being updated first (and all my tests have just never hit just right the conditions to have it be 2 )? As a followup question if the answer is " B will always

Is this defensive code or are there possibilities that it could encouter problems?

放肆的年华 提交于 2019-12-24 13:18:20
问题 I've got the following code which makes a connection to a db > runs a stored proc > and then moves on. I believe it is easy to get db programming wrong so it is important to be defensive : is the following defensive? (or can it be improved?) public int RunStoredProc() { SqlConnection conn = null; SqlCommand dataCommand = null; SqlParameter param = null; int myOutputValue; try { conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IMS"].ConnectionString); conn.Open(); dataCommand =

how to pass multiple values in single parameter and how to convert varchar value into integers

我是研究僧i 提交于 2019-12-24 12:22:33
问题 CREATE TABLE students ( id INT, NAME varchar(20) ) INSERT INTO students(id,name)VALUES(1,'Danny') INSERT INTO students(id,name)VALUES(2,'Dave') INSERT INTO students(id,name)VALUES(3,'Sue') INSERT INTO students(id,name)VALUES(4,'Jack') INSERT INTO students(id,name)VALUES(5,'Rita') INSERT INTO students(id,name)VALUES(6,'Sarah') This is my stored procedure alter PROCEDURE emp_sp ( @std_id as VARCHAR(500), @std_name as varchar(500) ) AS begin SELECT *FROM Students s WHERE s.id IN(convert(INT,@std

Bulk Insert Includes Line Terminator

删除回忆录丶 提交于 2019-12-24 12:13:02
问题 I am bulk importing data from a pipe-separated CSV file into SQL Server. The data is formatted like A|B|CCCCCC\r\n I have validated both that the file is in UTF-8 format and that lines are terminated with "\r\n" by viewing the CSV file in a hex editor. The command is BULK INSERT MyTable FROM 'C:\Path\File.csv' WITH (FIRSTROW=1, MAXERRORS=0, BATCHSIZE=10000, FIELDTERMINATOR = '|', ROWTERMINATOR = '\r\n') The third column originally was defined as CHAR(6) as this field is always a code exactly

SQL Server 2008 R2: Prepare a recursive query

 ̄綄美尐妖づ 提交于 2019-12-24 11:54:19
问题 I have the table with the two columns namely cola and colb as shown below: Table : Test create table Test ( cola int, colb int ); Records I have entered are: Cola Colb ------------ 1 2 1 3 1 4 2 5 2 6 2 3 3 2 3 4 3 7 3 10 10 11 11 12 11 13 11 14 12 15 13 16 14 99 15 88 16 77 Note : Now I want to show the only records who are connected with value I have pass. For example If I pass the value as 1 then it should display me the connected number to it and form connect like a tree. For this I am