sql-server-2012

SQL Prime number function

不打扰是莪最后的温柔 提交于 2019-12-18 21:48:30
问题 If I have a number X and want to say IsPrime(X) = true/false using sql-server what is the best approach? Do I just import a table of primes or is there an algorithm that is fairly efficient for the smaller primes? Note: I'm not interested in numbers greater than approx. 10 million. Ended up using the following: CREATE FUNCTION [dbo].[isPrime] ( @number INT ) RETURNS VARCHAR(10) BEGIN DECLARE @retVal VARCHAR(10) = 'TRUE'; DECLARE @x INT = 1; DECLARE @y INT = 0; WHILE (@x <= @number ) BEGIN IF

SQL Server 2012 PIVOT without aggregate

天涯浪子 提交于 2019-12-18 21:13:03
问题 I have the following sample data: Id Name Category ----------------------- 1 Joe A 2 Joe B 3 Joe D 4 Mary A 5 Mary C 6 Mary D I would like to show the categories a person belongs to like so: Name CategoryA CategoryB CategoryC CategoryD -------------------------------------------------- Joe X X X Mary X X X 1's and 0's could be used in place of X's and blanks. This smells like a PIVOT question to me. 回答1: There are several ways that you can transform the data. Some use an aggregate function

Set Script Task code dynamically in SSIS 2012

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 14:16:50
问题 In my application, a script task is created dynamically. In SQL Server 2008's implementation of SSIS, the following method worked fine. private void SetSourceCode(ScriptTask scriptTask, string code, string codeName) { string fileName = "ScriptMain.vb"; string language = "VisualBasic"; string proj = ".vbproj"; scriptTask.ScriptLanguage = VSTAScriptLanguages.GetDisplayName(language); scriptTask.ScriptingEngine.InitNewScript(language, scriptTask.ScriptProjectName, proj); scriptTask

SQL Server 2012 Install or add Full-text search

时间秒杀一切 提交于 2019-12-18 13:55:07
问题 I am working in SQL Server 2012, and would like to use the CONTAINS() function only it seems I need to have full-text search enabled for to be able to use it. How do I enable/install this feature to an existing SQL Server 2012 install? What I need are steps please as I am yet to find the steps on going about this. 回答1: You can add full text to an existing instance by changing the SQL Server program in Programs and Features . Follow the steps below. You might need the original disk or ISO for

Cannot start LocalDB

白昼怎懂夜的黑 提交于 2019-12-18 12:48:13
问题 Cannot start LocalDB instance, I have installed and re-installed over and over. Keep getting this error : Start of LocalDB instance "v11.0" failed because of the following error: Error occurred during LocalDB instance startup: SQL Server process failed to start. Anyone any advice ? ? Microsoft SQL Server 2012, i have ensured the FULL installation options, stopped all services SQL related and tried restarting . sqllocal info gives me : Projects v11.0 when i try sqllocal start v11.0 I get the

How do I add a “last modified” and “created” column in a SQL Server table?

痞子三分冷 提交于 2019-12-18 10:39:36
问题 I'm design a new db schema for a SQL Server 2012 database. Each table should get two extra columns called modified and created which should be automatically change as soon a row gets inserted or updated. I don't know how rather the best way to get there. I assuming that trigger are the best way to handle it. I was trying to find examples with triggers.. but the tutorials which I found insert data in another table etc. I assumed it's a quite common scenario but I couldn't find the answer yet.

T-SQL: Separate String Into Multiple Columns

戏子无情 提交于 2019-12-18 09:28:25
问题 Ex. Column 1: | word1 word2 word3 word4 | to Col 1: Col 2: Col 3: Col 4: | word1 | word2 | word3 | word | Is it possible to separate different words or phrases from a string into multiple columns? All words and phrases in the strings are usually separated by double spaces, nothing else. Is there a pre-defined function I can use already available from SQL Server like CAST or INTERSECT, or do I have to write my own? 回答1: here is a dynamic sql version. of John's in case you don't know the

SQL Server 2012 : extract Regex groups

感情迁移 提交于 2019-12-18 09:03:45
问题 I have text in my database in Markdown format. I'd like to extract links and count the number of matching links I have. I can get a listing of text blocks that contain links using a query similar to this: SELECT post_text FROM posts p WHERE p.body like '%\[%](http%)%' ESCAPE '\' How do I go to the next step though, and just extract the link portion of the text (the part that is in the parenthesis)? If I can get this, I can count the number of times this specific link is in my dataset. Some

Why doesn't xp_cmdshell work in SQL Server 2012? [duplicate]

拟墨画扇 提交于 2019-12-18 05:57:25
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Enable ‘xp_cmdshell’ SQL Server When I run xp_cmdshell command in SQL Server 2012, I get the following message: SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp

Cannot initialize the data source object of OLE DB provider “Microsoft.ACE.OLEDB.12.0” for linked server “(null)”

∥☆過路亽.° 提交于 2019-12-18 04:27:09
问题 When I run the following commmands in SQL Server 2012 exec sp_configure 'Advanced', 1 RECONFIGURE exec sp_configure 'Ad Hoc Distributed Queries', 1 RECONFIGURE EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1 SELECT * INTO dbo.Normalization FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=c:\_sandbox\Consolidations.xlsx','SELECT * FROM [Codes and Values