sql-server-2012

Can you set a stored procedure to always execute as a specific user?

假装没事ソ 提交于 2019-12-24 07:07:19
问题 I would like to set a stored procedure to always execute as a specific domain user. Regardless of the user calling or trying to execute the procedure, can I force the procedure to be executed as another user. One of our vendors has their app hardcoded to use a local db account to execute certain procedures to import some csv files. Unfortunately we cannot be storing the csv files on the local database server and need to place them on a network share. Therefore I need to set these procedures

Transpose in SQL Server 2012

ε祈祈猫儿з 提交于 2019-12-24 07:04:03
问题 I have the following: Country | StateCity --------+------------ USA | MO USA | LA USA | OH CANADA | Ontario CANADA | Toronto and am looking to transpose and unify the header with a result like USA CANADA MO Ontario LA Toronto OH 回答1: But if you want DYNAMIC: Sql DEMO First you need a temporal table to create a row_id SELECT row_number() over (partition by [Country] order by [StateCity]) [rn], [StateCity], [Country] INTO temp FROM State; Then you can go the dynamic pivot route Create the

Display an image stored as blob in SQL Server

拈花ヽ惹草 提交于 2019-12-24 06:45:03
问题 I have a query to fetch info images : $q4 = "SELECT TOP 3 b.BadgeName, b.BadgeImage FROM BadgeImageTable AS b INNER JOIN employee_badge AS e ON e.badge_id = b.BadgeID WHERE e.employee_id = 2164 ORDER BY e.earned_on DESC "; $stmt3=sqlsrv_query($conn,$q4); if($stmt3==false){ echo 'error to retrieve info !! <br/>'; die(print_r(sqlsrv_errors(),TRUE)); } HTML previous: <!-- <span class="fa-stack fa-5x has-badge" > <div class="badgesize"> <img src="images/1.png" alt="" > </div> </span> --> Now in

YEARFRAC function on SQL Server not working

故事扮演 提交于 2019-12-24 03:07:20
问题 I was happy to find you can use an Excel-like YEARFRAC function in MS SQL server (http://technet.microsoft.com/en-us/library/ee634405.aspx), but for some reason I get an error that states: 'yearfrac' is not a recognized built-in function name when I try to run my query. Here is my code: SELECT CUSTOMER_ID, PRODUCT_SKU, SUB_START_DATE, SUB_EXP_DATE, YEARFRAC(sub_start_date, sub_exp_date) AS START_TO_END FROM ... For the record, I have double-checked the dates are in proper datetime format, and

SQL Server Query Time Out

霸气de小男生 提交于 2019-12-24 02:23:30
问题 A 3rd party application accessing a SQL Server 2012 database is getting [Microsoft][ODBC SQL Server Driver]Query timeout expired errors after executing for about 20 mins. This is what I see on the database after the application starts receiving the errors: SPID 102 is not shown in the query above. It is another connection from the same application for the same process. I was able to capture some details of this in the screenshot below. It is the one in the topmost row in this screenshot.

SQL - Create a temp table or CTE of first day of the month and month names

寵の児 提交于 2019-12-24 01:55:09
问题 I need to create a temp table or common table expression based on 2 paremters in a SQL Server 2012 environment @calYear @currentYear so if @calYear = 5 and @currentYear='2014' I would like to generate a temp table of 5 years starting from current year with 4 columns like YearDesc MonthName MonthNum FirstDayOfMonth 2014 Jan 1 1/1/2014 2014 Feb 2 2/1/2014 2014 Mar 3 3/1/2014 ... ... ... 2018 Oct 10 10/1/2018 2018 Nov 11 11/1/2018 2018 Dec 12 12/1/2018 Is it possible to do a Do While loop

DROP PROCEDURE throws syntax error

泄露秘密 提交于 2019-12-24 01:41:33
问题 I have a #temp table where I have names of stored procedures. DECLARE @object VARCHAR(200) SET @object = (SELECT Top 1 Te.[this Object...] From #Temp Te) IF OBJECT_ID(@object) IS NOT NULL DROP PROCEDURE @object But on last step I get error Incorrect syntax near '@object'. Is this because @object is of type VARCHAR or what ? This is for SQL Server 2012 回答1: you can't do it like that. You need to use dynamic sql. Something like : DECLARE @object NVARCHAR(200) DECLARE @sql NVARCHAR(max) SET

SQL Keeping count of occurrences

早过忘川 提交于 2019-12-24 01:41:23
问题 I've the following problem I need to solve in SQL. Let's say that I have a table with 2 columns: Date | Code -------- 0 | 25 1 | 22 2 | 23 3 | 25 4 | 23 5 | 21 And I need to keep a count of them, as the Date is relevant for me. So, let's say that I would need to produce something like this: Date | Code | Count -------------------- 0 | 25 | 1 1 | 22 | 1 2 | 23 | 1 3 | 25 | 2 4 | 23 | 2 5 | 21 | 1 Thanks in advance, PS: I'm implementing it in MSSQL 2012. Regards. 回答1: The simplest (and probably

WCF Service stop responding unexpectedly with error “client authentication scheme 'Anonymous'.The remote server returned an error: (403) Forbidden.”

五迷三道 提交于 2019-12-24 01:25:45
问题 we developed a WCF service ( running on windows server 2012) and client API which are connected each other with client certificate authentication. They were running cool up to we made a stress test on system. We try to load system with giant data then the system responds so slowly. So we interrupt the process and try to clear DB. The clearing and reindexing DB took more than 6 hours. After that the WCF service stoped responding! But we did not change any code block! Therefore, I think that

replace only matches the beginning of the string

回眸只為那壹抹淺笑 提交于 2019-12-24 00:55:57
问题 I'm trying to write a function to replace the Romanian diacritic letters ( ĂÂÎȘȚ ) to their Latin letter equivalents ( AAIST , respectively). SQL Server's replace function deals with Ă , Â , and Î just fine. It seems to have a weird problem with Ș and Ț , though: they are only replaced if they are found at the beginning of the string. For example: select replace(N'Ș', N'Ș', N'-') -- '-' # OK select replace(N'ȘA', N'Ș', N'-') -- '-A' # OK select replace(N'AȘ', N'Ș', N'-') -- 'AȘ' # WHAT??