sql-server-2008-r2

Reflection in SQL Server 2008?

别说谁变了你拦得住时间么 提交于 2019-12-11 02:51:13
问题 I want to know, is there any reflection support in SQL Server 2008, like as c# support reflection. Basically I am curious about how SQL Server implements all our (where, orderby, exists) clauses. How it would implement all these in behind the scenes. 回答1: It's not that long ago when if you compared SQLServer to most object-oriented languages, that you would have been struck by the fact that SQLServer reveals a lot more about its inner workings than they do. Its inherent to the concept of SQL

How do I model one entity that references one of several other entities in SQL?

China☆狼群 提交于 2019-12-11 02:41:29
问题 I am working on a portion of a manufacturing DB. The business takes in custom orders and builds items to spec. They only build several (let's say 3-10) types of objects, but each type of object is different in the specifications that get recorded. I want to have a master manufacturing table (mfgorders) that lists some common fields and then have it refer to a specifications table that is specific to the entity ordered. I'm not entirely confident this is the right approach. In fact, I'm not

Sqlserver table Row to column data - pivot table

匆匆过客 提交于 2019-12-11 02:37:43
问题 I have an Excel-sheet as below, I already dump the data into database (as the sample data below) from this excel in normalized form. Now I want to get the similar view of excel from database's data. I tried this, but given in wrong format. Good if somebody given the same result view as excel with column name and inner join. I do not want to hardcore as the year expand. declare @tblyear table(id int, year int) insert into @tblyear values (1,2012), (2,2013),(3,2014) ,(4,2015),(5,2016) declare

OpenJPA 1 - sequence table not being created

有些话、适合烂在心里 提交于 2019-12-11 02:24:05
问题 I have an entity class with the following annotation on its primary key: @GeneratedValue(strategy = GenerationType.AUTO) . However, when I try to persist an instance of this class, I get com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'OPENJPA_SEQUENCE_TABLE'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197) The table it's looking for definitely does not exist in the database. The user it's connecting to the database as

Calculations over Multiple Rows SQL Server

旧巷老猫 提交于 2019-12-11 02:22:42
问题 If I have data in the format; Account | Period | Values Revenue | 2013-01-01 | 5432 Revenue | 2013-02-01 | 6471 Revenue | 2013-03-01 | 7231 Costs | 2013-01-01 | 4321 Costs | 2013-02-01 | 5672 Costs | 2013-03-01 | 4562 And I want to get results out like; Account | Period | Values Margin | 2013-01-01 | 1111 Margin | 2013-02-01 | 799 Margin | 2013-03-01 | 2669 M% | 2013-01-01 | .20 M% | 2013-02-01 | .13 M% | 2013-03-01 | .37 Where Margin = Revenue - Costs and M% is (Revenue - Costs)/Revenue for

How to update the column without loop in SQL Server?

社会主义新天地 提交于 2019-12-11 01:51:22
问题 Due to performance perspective I just need to remove loop and using some joins or other solution to update the data in #Result table and get the same result which return by loop. Scalar function: CREATE FUNCTION [MultiplyerScl] (@a INT, @b INT) RETURNS INT AS BEGIN DECLARE @i AS INT = 2 DECLARE @Value AS INT IF (@b % @a = 0) BEGIN SET @Value = @b END ELSE BEGIN WHILE (1=1) BEGIN IF((@b * @i) % @a = 0) BEGIN SET @Value = @b * @i BREAK; END ELSE BEGIN SET @i = @i + 1 END END END RETURN @Value

Use CASE statement with SUM function in SQL Server

不问归期 提交于 2019-12-11 01:44:51
问题 I am working on SQL Server 2008 R2. I am trying to get the sum. This is my query select SUM( case when sec.SecurityTypeID = 2 then SUM(quantity)*(sec.AnnualIncomeRate/100) when sec.SecurityTypeID = 5 then 0 when sec.SecurityTypeID = 11 then SUM(quantity)*sec.AnnualIncomeRate else SUM(quantity)*sec.AnnualIncomeRate end ) AS ProjectedIncome from Transactions as t When I execute it give me following error. Msg 130, Level 15, State 1, Line 3 Cannot perform an aggregate function on an expression

How to use column name as part of a LIKE statement in a WHERE clause of a JOIN

▼魔方 西西 提交于 2019-12-11 01:27:25
问题 LEFT OUTER JOIN [INVENTTRANS] ON #TEMP.VOUCHERPHYSICAL=[INVENTTRANS].VOUCHERPHYSICAL WHERE [INVENTTRANS].ITEMID = #Temp.INVENTDIMID This provides me nearly the result I am looking for. About 1/4th of the desired results are eliminated because INVENTDIMID has a suffix appended to it in certain cases. What is the proper syntax to succeed in a like clause similar to this intent. WHERE '[INVENTTRANS].ITEMID%' like #Temp.INVENTDIMID Alternatively, if there is no short hand way to do this, what is

Replace all characters of string to asterisks except first characters and space in MSSQL

余生长醉 提交于 2019-12-11 01:26:34
问题 I`m was wonder if it is possible to hash sensitive data in mssql via function and to leave first character “as-is” for all the fields. Example: "Jon Kirk” name should be extracted as J** K*** 回答1: I had posted a recursive solution at first. This is faster: declare @name varchar(20) = 'Jon Kirk' declare @loop int = len(@name) while @loop > 1 select @name = stuff(@name, @loop, 1, case when substring(@name, @loop-1,2) like '% ' then ' ' when substring(@name, @loop-1,2) like ' %' then substring(

How to drop tables based on sys.objects? [duplicate]

会有一股神秘感。 提交于 2019-12-11 01:15:28
问题 This question already has an answer here : How to delete all tables with prefix “bkp” from a given database? (1 answer) Closed 6 years ago . I am trying to drop tables from each server/DB. I ran the query to get the list of the tables in each database from different server. SELECT * FROM sys.objects WHERE type = 'u' AND name LIKE '%JSK%' I want to drop those tables. I need query how to do it? 回答1: Assuming no foreign key relationships make order of dropping important: DECLARE @sql NVARCHAR