sql-server-2012

How to write a select inside case statement

别来无恙 提交于 2019-12-04 04:57:21
问题 I have a stored procedure that contains a case statement inside a select statement. select Invoice_ID, 'Unknown' as Invoice_Status, case when Invoice_Printed is null then '' else 'Y' end as Invoice_Printed, case when Invoice_DeliveryDate is null then '' else 'Y' end as Invoice_Delivered, case when Invoice_DeliveryType <> 'USPS' then '' else 'Y' end as Invoice_eDeliver, Invoice_ContactLName+', '+Invoice_ContactFName as ContactName, from dbo.Invoice left outer join dbo.fnInvoiceCurrentStatus()

Get family members

半腔热情 提交于 2019-12-04 04:26:34
Supose the families bellow: The Build Schema of this is: create table PersonConn (child int, parent int) insert into PersonConn values (1,2) insert into PersonConn values (1,3) insert into PersonConn values (5,3) insert into PersonConn values (5,4) insert into PersonConn values (6,7) insert into PersonConn values (6,8) insert into PersonConn values (2,9) insert into PersonConn values (2,10) insert into PersonConn values (3,11) insert into PersonConn values (3,12) To get the ancestors of a family member I can use recursion as showed bellow: WITH Childs AS ( SELECT distinct Child, Parent FROM

Use TO_DATE in sql server 2012

烂漫一生 提交于 2019-12-04 04:06:38
问题 I have a problem. When I execute the next sentence in the SQL server 2012: TO_DATE('2011-11-09 00:00:00','YYYY-MM-DD HH24:MI:SS') I receive to the error: 'TO_DATE' not is a name de function integrate recognized. What is the solution? Thanks! 回答1: SQL-Server has no TO_DATE function. You have to use convert . See here -- Specify a datetime string and its exact format SELECT TO_DATE('2012-06-05', 'YYYY-MM-DD') FROM dual; -- Specify a datetime string and style 102 (ANSI format), raises an error

Powershell - User Mapping SQL Server 2012

落花浮王杯 提交于 2019-12-04 04:05:10
I am trying to script User Mapping for different Login accounts. I have scripted the creation of users and individual server roles, but I can't figure out how to set User Mapping with Powershell, I will also need to set the Database Role membership , in Particular, db_backupoperator Anyone know how to do this with Powershell? Supposing your login is created ## Creating database user and assigning database role #get variables $instanceName = "yourInstance" $loginName = "testLogin" $dbUserName = "testUserName" $databasename = "tempdb" $roleName = "db_backupoperator" $server = New-Object

Entity Framework 5 and Amazon RDS - “The underlying provider failed on Open.”

╄→гoц情女王★ 提交于 2019-12-04 04:03:43
问题 I have a C# / Entity Framework web application runs fine against a local SQL 2012 db. I copied the db out to a new RDS instance, and can access the db via Visual Studio and SQL Server Management Studio. I hav a unit test which authenticates the user and then attempts inserts a record in a table using an Entity Framework call -- dataContext.SaveChanges(). I get the following error: {"The underlying provider failed on Open."} {"No such host is known"} {"A network-related or instance-specific

Linq query not behaving as expected

大憨熊 提交于 2019-12-04 03:01:21
问题 I have a very simple linq query which is as following: var result = (from r in employeeRepo.GetAll() where r.EmployeeName.Contains(searchString) || r.SAMAccountName.Contains(searchString) orderby r.EmployeeName select new SelectListItem { Text = r.EmployeeName, Value = r.EmployeeName }); The issue is for some strange reason it fetches me the record of every person who I search for whether in lower case or upper case. i.e. test user Test User TEST USER I will get back the correct records.

How to get the next Sequence number in Sql Server Express using Entity Framework?

六眼飞鱼酱① 提交于 2019-12-04 02:59:30
Now that Sql Server 2012 (including SQL Server Express 2012) has SEQUENCE feature just like Oracle as explained here , here , and here . I can get the next sequence like so, SELECT NEXT VALUE FOR SeqName But how do I do that from my code using Entity Framework 5? I got it working using SqlQuery like so.. int sequence = context.Database.SqlQuery<int>("SELECT NEXT VALUE FOR MySequenceName").FirstOrDefault(); 来源: https://stackoverflow.com/questions/16753149/how-to-get-the-next-sequence-number-in-sql-server-express-using-entity-framework

How to connect to SQL Server LocalDB using Invoke-Sqlcmd?

扶醉桌前 提交于 2019-12-04 02:58:24
I have sqlcmd.exe from both SQLServer 2008 and SQLServer 2012: PS C:\> Get-Command sqlcmd.exe Definition ---------- C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE By modifying $env:PATH i force the use of sqlcmd.exe from SQL Server 2012: PS C:\> $env:PATH = ($env:PATH -split ";" | Where-Object { $_ -notlike "*\Microsoft SQL Server\100\*" }) -join ";" PS C:\> Get-Command sqlcmd.exe Definition ---------- C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE The default instance of LocalDB is up and running

SQL: Return Column names where column contains a given Value

橙三吉。 提交于 2019-12-04 02:35:37
问题 i was wondering if there was a command/stored proc i can run in SQL Server that will give me the names of columns that contain given data within a table. So if i was to query, give me all the columns in this table, that contain the value 75. i wouldnt want the row. just the column name within the table... is this possible? 回答1: -- input parameters (guessing on type for @value): DECLARE @schema SYSNAME = N'dbo', @table SYSNAME = N'z', @value VARCHAR(64) = '75'; -- now, inside the procedure

SELECT FOR XML AUTO and return datatypes

半世苍凉 提交于 2019-12-04 01:25:13
During playing with sys.dm_exec_describe_first_result_set I get to this point: CREATE TABLE #tab(col INT, x XML ); INSERT INTO #tab(col,x) VALUES (1,NULL), (2,NULL), (3,'<a>x</a>'); SELECT 'Simple XML' AS description, name, system_type_name FROM sys.dm_exec_describe_first_result_set( N'SELECT col FROM #tab FOR XML AUTO', NULL, 0) UNION ALL SELECT 'Wrapped with subquery', name, system_type_name FROM sys.dm_exec_describe_first_result_set( N'SELECT(SELECT col FROM #tab FOR XML AUTO) AS wrapped_subquery', NULL, 0) UNION ALL SELECT 'XML column', name, system_type_name FROM sys.dm_exec_describe