sql-server-2008-r2

Altering Multiple Tables at once

筅森魡賤 提交于 2019-12-11 03:55:02
问题 I'm trying to alter multiple SQL Server 2008 R2 tables at one time. This is my code: use DatabaseName go Declare @SchemaUsed varchar(20) = 'dbo' create table #Tables ( TableName varchar(100), Processed int ) insert into #Tables select top 1 table_name, 0 from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = @SchemaUsed and table_type = 'Base Table' and (TABLE_NAME like 'PM%' ) ORDER BY TABLE_NAME DECLARE @TableName varchar(max) DECLARE @SQL varchar(max) WHILE EXISTS (select top 1 'x' from

Are Columns Not Selected in SQL Views Executed?

你。 提交于 2019-12-11 03:54:49
问题 I wasn't able to come up with the right keywords to search for the answer for this, so apologies if it was answered already. Consider the following SQL view: CREATE VIEW View1 AS SELECT Column1 ,Column2 ,(SELECT SUM(Column3) FROM Table2 WHERE Table2.ID = Table1.ID) -- Subquery FROM Table1 If I run the following query, will the subquery be executed or does SQL Server optimise the query? SELECT Column1 FROM View1 I'm looking at this from a performance point of view, say, if the view has quite a

SQL return consecutive records

ⅰ亾dé卋堺 提交于 2019-12-11 03:37:34
问题 A simple table: ForumPost -------------- ID (int PK) UserID (int FK) Date (datetime) What I'm looking to return how many times a particular user has made at least 1 post a day for n consecutive days. Example: User 15844 has posted at least 1 post a day for 30 consecutive days 10 times I've tagged this question with linq/lambda as well as a solution there would also be great. I know I can solve this by iterating all the users records but this is slow. 回答1: There is a handy trick you can use

unsigned short data type in my Sql server 2008 r2

我是研究僧i 提交于 2019-12-11 03:36:47
问题 I want to store Port numbers in my SQL server database. In general and any port can have values from (0 to 65,535). And on the following link http://msdn.microsoft.com/en-us/library/s3f49ktz%28v=vs.71%29.aspx it is mentioned that “unsigned short” will be suitable for storing Port number. But in my case I am suing Sql server 2008 r2, so which data type I can use to represent “unsigned short”? Regards 回答1: See the documentation on data types. You could use a decimal/numeric: Precision Storage

Comma Separated list of rows of a column with group by on other columns

浪尽此生 提交于 2019-12-11 03:23:15
问题 Below is the structure of table I have: - Table T1 C1 C2 C3 ---------- X P A X P B Y Q C Y Q D Desired output: - C1 C2 C3 ------------ X P A,B Y Q C,D Note: - I know i can do the same with For XML('') with group by on C1 and C2, but the main problem in my case is that the table T1 here must be a physical table object (either permanent or temp or table var or CTE) in DB. But in my case it's a derived table and when i am using the below query it's saying invalid object. In my case it's not good

Last 4 Whitespaces of Sql Would Hurt Sql Server Performance?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 03:21:21
问题 I have run a weird problem. Maybe someone here can help me. My SQL Server version is 2008 R2. It runs at a server with 24 cores. I have 2 query strings: String SQL_1 = "select t.testconfig_id, t.minuteSequence, t.location_id, sum(t.vuPerNode) as totalVu, sum(t.backOffPctSum) / sum(t.recordNum) as avgBackOffPct from (select p.testconfig_id, p.minuteSequence, r.location_Id, SUM(p.activeCount) * 1.0 / COUNT(1) as vuPerNode, SUM(p.backOffPct) as backOffPctSum, COUNT(1) as recordNum from loadtest

Updating one SQL table based on data in another table

╄→гoц情女王★ 提交于 2019-12-11 03:18:03
问题 I am running Microsoft SQL Server 2008 R2, and pulling information from two tables to create one new table. Table A has leads with a unique lead number and other information. Table B has sales with a unique sales number, and the lead number associated with it. Data from both tables are pulled into temp tables in SQL Server so I can change and update whatever I need, and the output of this will go into a new table. One lead from Table A can have multiple sales associated with it in table B. I

SQL Combine multiple rows into one with multiple columns

自作多情 提交于 2019-12-11 03:17:04
问题 I've got multiple rows in my result from my query: for example, the table "Address": Street | Number | City ---------------------- A1 | A2 | A3 B1 | B2 | B3 What I actually want is: Address1_Street | Address1_Number | Address1_City | Address2_Street | Address2_Number | Address2_City ------------------------------------------------------------------------------------------------------ A1 | A2 | A3 | B1 | B2 | B3 Anyone who knows how I can achieve this? I've managed to get to this point now

different estimated rows on same index operation?

时光毁灭记忆、已成空白 提交于 2019-12-11 03:08:45
问题 Introduction and Background I had to optimize a simple query (example below). After rewriting it several times I recognized that the estimated row count on the one and same index operation differs depending on the way the query is written. Originally the query did a clustered index scan, as the table in production contains a binary column the table is quite large (about 100 GB) and the full table scan takes too much time to execute. Question Why is the estimated row count different on the

A CLR function equivalent to string.Format in C#

不羁岁月 提交于 2019-12-11 02:54:52
问题 I was looking for a CLR function that will do the same thing as String.Format in C#. As an example, I would like to do the following through a CLR function: String.Format("My name is {0}, and I live in {1}",myName, cityName) I would like to be able to pass variable number of parameters after the first parameter, which would be as many place holders are specified in the first parameter to this CLR function. Anyone has any idea on what would be the C# code for such a CLR function? 回答1: I'm