tsql

create index using openquery

一世执手 提交于 2021-02-10 18:44:24
问题 How do I create an index on a table that exist in a remote SQL Server database using the openquery syntax? 回答1: You can't on your side. The index must be added to a local object only. You can't use an indexed view either. You can ask the other party to add an index for you to their table... Edit: Expanding John's answer... You could try: SELECT * FROM OPENQUERY(LinkedServer, 'CREATE INDEX etc;SELECT 0 AS foobar') 回答2: I'm not certain however I suspect that this cannot be done. OPENQUERY is

Scripts in SQL are compiled or executed

随声附和 提交于 2021-02-10 17:29:45
问题 This is like a little existential doubt... Which is the right way to say that you run a script in SQL? I compiled the script xxxx I executed the script xxxx I know is a little weird and noob to ask this, but I need to know. I'm very sure the second way is the correct one, but I need more opinions. Thank you very much. 回答1: It depends on what you actually want to say. All queries in SQL Server follow the same process before they are executed. Parse - Statement is broken down into individual

Same query giving different results

冷暖自知 提交于 2021-02-10 16:21:31
问题 I am still new to working in databases, so please have patience with me. I have read through a number of similar questions, but none of them seem to be talking about the same issue I am facing. Just a bit of info on what I am doing, I have a table filled with contact information, and some of the contacts are duplicated, but most of the duplicated rows have a truncated phone number, which makes that data useless. I wrote the following query to search for the duplicates: WITH CTE (CID,

(T-SQL) Why does this subquery need an alias?

北慕城南 提交于 2021-02-10 16:17:34
问题 SELECT * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY a.vendorid ORDER BY a.CreatedDateUTC) as RowNum ,* FROM ZpVendors_Kim.dbo.VendorPaymentAcceptanceAudit a) Needs_Alias_Here WHERE RowNum = 1 Very simple query, just wondering - why is an alias needed for it to work? 回答1: The alias after the subquery (or derived table, if you prefer) is required by SQL Server. It is not only a requirement but a really good idea. In general, column references should be qualified , meaning that they include a

Same query giving different results

假装没事ソ 提交于 2021-02-10 16:11:44
问题 I am still new to working in databases, so please have patience with me. I have read through a number of similar questions, but none of them seem to be talking about the same issue I am facing. Just a bit of info on what I am doing, I have a table filled with contact information, and some of the contacts are duplicated, but most of the duplicated rows have a truncated phone number, which makes that data useless. I wrote the following query to search for the duplicates: WITH CTE (CID,

SQLCLR .NET Error: Object reference not set to an instance of an object

女生的网名这么多〃 提交于 2021-02-10 14:17:28
问题 I am currently receiving an error when trying to execute a simple SELECT statement that references an assembly that contains the C# code for the Jaro-Winkler distance algorithm. This is my first time working with SQLCLRs. I am able to run the code successfully below without the additional OR statement Example: SELECT * FROM USERS WHERE ( DATE_OF_BIRTH IS NOT NULL AND DBO.JAROWINKLER(CONVERT(VARCHAR(6),DATE_OF_BIRTH,12),@DOB) > 0.9 ) OR ( USERID = @USERID ) However when I include the OR

Referencing a previous row value for an arithmetic calculation in SQL Server 2008 R2

断了今生、忘了曾经 提交于 2021-02-10 06:32:54
问题 I am working with SQL Server 2008 R2 and new to relational database. I need to run a simple calculation but the calculation involves using a previous row value. Example: (Value of X) / ((Value of Y at time t + Value of Y at time t-1) / 2) Example: select (x/[(y@time,t + y@time,t-1)/2]) as 'Value' from datatable select ((c.ACHQ)/(c.RECTQ(row:n) + c.RETQ(row:n-1))/2) as 'AR' from co_ifndq c where c.GVKEY in (select GVKEY from spidx_cst where DATADATE = '2012-03-12' and INDEXID = '500') and c

How to convert custom string to Date in SQL Server

自古美人都是妖i 提交于 2021-02-10 06:14:38
问题 How to convert yyyyMMddhh ( 2017092018 ) string to Date in SQL Server 2012? Is it possible to do without using T-SQL to put the sentence into the INSERT clause? INSERT INTO [dbo].[Table](SomeColumn) VALUES (CONVERT(DATETIME, '2017092018', 'yyyyMMddhh')); 回答1: Example Declare @S varchar(50)='2017092018' Select convert(datetime,left(@S,8)) + convert(datetime,right(@S,2)+':00') Returns 2017-09-20 18:00:00.000 If 2012+, I would suggest try_convert() just in case you have some unexpected values.

Break up monthly data into daily

微笑、不失礼 提交于 2021-02-10 05:32:04
问题 I have budget data for a company in the following montly format. SqlFiddle link here. Dept# YearMonth Budget($) -------------------------- 001 201301 100 001 201302 110 001 201303 105 .. ..... ... 002 201301 200 ... ..... ... I am required to break this up into daily records, which would look like this: Dept# Date Budget($) -------------------------- 001 20130101 xxx 001 20130102 xxx 001 20130103 xxx .. ..... ... I need to generate daily records from each record in the source table. I don't

SQL Server Stored Procedures: do they queue up?

情到浓时终转凉″ 提交于 2021-02-08 23:36:31
问题 I should probably be able to find an answer to this but my Google-fu is weak today. When I have the same stored proc called multiple times by a web app, do the calls queue up or do they run independently? 回答1: Depends on the isolation level of what the stored procedure is doing. If the isolation level is set to READ UNCOMMITTED for all the transactions in the SP, there is no protection, and multiple threads can be performing the same transaction at the same time. If it's set to a higher