sql-server-2012

Distinct error with image field

瘦欲@ 提交于 2019-12-11 23:55:26
问题 I have table which contains a Image column. Now I want to select distinct value of image column, but it gives error. Column name is also Image. My query is: select Image from tbl1 Error is: The image data type cannot be selected as DISTINCT because it is not comparable. So how to select distinct value from tb1 table 回答1: Image data types in a SELECT statement that contains the DISTINCT clause. Depending on the version of SQL Server being used, there are a few ways of overcoming this

Query XML data in a function with namespace

丶灬走出姿态 提交于 2019-12-11 22:19:24
问题 I am facing problems while retrieving data from XML. with xmlnamespaces ('x-elements' as x) select tb.[Profile].value('(x:ppr/x:static/refId)[1]', 'varchar(22)') testCol from table1 tb The above code works perfectly fine. But, when I pass the XML path in a function it compiles correctly but doesn't return data upon calling but just the xml path (that is passed to it). CREATE FUNCTION testFunc ( @p varchar(22) ) RETURNS nvarchar(max) AS BEGIN DECLARE @Rs nvarchar(max); with xmlnamespaces ('x

Rename column returning error

[亡魂溺海] 提交于 2019-12-11 21:21:13
问题 I tried using this procedure to rename badly named columns in SQL Server. The generated statement seems correct: EXEC sp_rename '[TBL_TAXREPORTtestxxx].["InsertedOn"]', 'InsertedOn', 'COLUMN' however that gives me the following error: Msg 15248, Level 11, State 1, Procedure sp_rename, Line 266 Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong. Any clue ? 回答1: Option 1: You can try to check if you are running the query in the correct database. Option 2: If

Pass datetime to stored procedure

旧时模样 提交于 2019-12-11 20:13:50
问题 Using sql server 2012. I'm having a hard time passing a date parameter to the stored procedure. The column's datatype in the table is datetime . alter procedure savedate() ( @StartDate datetime ) The text from the source textbox is 2014-09-18. The below code will give an error - string not recognized as valid datetime SqlParams[0] = new SqlParameter("@StartDate", SqlDbType.DateTime); SqlParams[0].Value = Convert.ToDateTime(txtStartDate.Text.Trim());//error here And this will say - Failed to

Why do I need the 'match' part of a SQL merge, in this scenario?

丶灬走出姿态 提交于 2019-12-11 20:09:38
问题 Consider the following: merge into T t1 using (select ID,Col1 from T where ID = 123) t2 on 1 = 0 when not matched then insert (Col1) values (t2.Col1); Cominig from a programming background, to me this translates to: "Evaluate false (i.e. 1 = 0), and when it is false (i.e. all the time), insert." Is it not possible to just omit the match condition? Is it because of my select's where condition that I'm confused here? Should this condition be moved to the on ? NOTE: Due to restrictions with

Why XML column is greyed out?

风格不统一 提交于 2019-12-11 19:53:27
问题 why XML column doesn't let me put anything in it and greyed out, I can populate other columns ID A or ID B I can add values but with Xml it doesn't let me add anything, even " <element> " ? Creating a table like this, USE [DBNAME] GO DROP TABLE [dbo].[TableName] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[TableName]( [Id] [int] IDENTITY(1,1) NOT NULL, [ID_A] [int] NULL, [ID_B] [int] NULL, [Xml] [xml] NULL, CONSTRAINT [PK_TableName] PRIMARY KEY CLUSTERED ( [Id] ASC

Give permissions to a stored procedure

自古美人都是妖i 提交于 2019-12-11 19:37:26
问题 I have a stored procedure that does a merge into a table. Let's call it someSchema.UpsertSomeStuff (note that it is not in the dbo schema). Let's call the table dbo.SomeStuff (note that it is in the dbo schema). When I call the stored procedure with a data reader/data writer user, it works fine. But if I give permissions to call this stored procedure to a very weak user (no data writer or reader rights), then it fails telling me that I can't select, insert or update to dbo. SomeStuff . I don

SQL Alternative to IN operator with variable and between

妖精的绣舞 提交于 2019-12-11 19:23:34
问题 I'm trying to put some variables to an IN Operator but unfortunately it's not working. (found some articles on the internet about it). So what can I do? I have some stacked queries combined to one. But then I have several times the same argument, well that's no problem. But I have to change all the arguments every times I want to make a new search. So to make it easy I used variables at the top of the query so I can't forget even one to change. And that's working also except for the IN

Select only last value of date?

巧了我就是萌 提交于 2019-12-11 19:08:45
问题 I have a SELECT, that returns me something similar to this result (I've simplified it, because the original SELECT is rather complex): | UserFK | aDate | aValue | --------------------------------------------- | 1000 | 03.12.2012 10:00 | 123 | | 1000 | 03.12.2012 11:00 | 456 | <-- | 1001 | 03.12.2012 09:00 | 111 | <-- | 1002 | 03.12.2012 09:00 | 222 | | 1002 | 03.12.2012 10:00 | 333 | <-- | 1003 | 03.12.2012 09:00 | 123 | | 1003 | 02.12.2012 09:00 | 123 | | 1003 | 03.12.2012 10:00 | 455 | <--

maintaining a custom auto increment column

↘锁芯ラ 提交于 2019-12-11 18:58:51
问题 I have a table with ID (identity) and XID (int) which is my custom auto increment column. I am using an instead of insert trigger to maintain XID, but I am getting duplicates. table xtable (ID identity, XID int) trigger - instead of insert insert into [xtable] (XID) select [x].[NextavailableID] from inserted [i] cross apply ( select coalesce(max([t].[XID]), 0) + 1 [NextavailableID] from [xtable] [t] ) [x]; Assuming inserted = 1 row. This trigger doesn't prevent duplicates in the XID column.