sql-server-2008-r2

How to replace fetched values with another column while querying with SQL Server 2008 R2

为君一笑 提交于 2019-12-01 21:47:56
Alright let me explain my question with example We have 1 table This table contains Id Name Number Now example 1 House 4 2 Hospital 3 3 Airport 1 4 Station 2 Now when fetching as select * from table I want to replace third column number values with that number representing Name So example of fetching 1 House Station 2 Hospital Airport 3 Airport House 4 Station Hospital How can i do this ? thank you select t1.id, t1.name, t2.name as name2 from your_table t1 left join your_table t2 on t1.number = t2.id You can join the same table twice to replace the number with the name . The on contidion in

What are all the possible first-words of SQL statements?

一曲冷凌霜 提交于 2019-12-01 21:27:44
I'm building a user interface to be able to execute SQL statements on a SQL Server database, compatibility at SQL Server 2008 R2. I need to be able to determine whether each statement could possibly return a dataset, or if it just needs to be executed. In Delphi, the TADOQuery consists of either Open / Close for a dataset, or ExecSQL just to execute. I need to automatically determine which one to use based on the first word(s) of the SQL statement. How can I determine which method I should call based on the first word(s) in the statement? I would need to know each possible word, and which

Ad hoc updates to system catalogs are not allowed

不打扰是莪最后的温柔 提交于 2019-12-01 20:56:05
So I'm trying to manually update hash for one user. I tried updating view, as tables are not visible. I tried starting in single user mode, changing "sp_configure 'Allow updates',1", and so on, with no luck. Is there any way. I don't know passwords, just hashes, as updating from 2000 SP4 to 2008 R2 failed, and I need to have credentials on 2008 R2, just like on old 2000. steoleary The sp_Configure 'Allow Updates' setting has had no effect since SQL Server 2005, it will still "work" but it won't do anything. I believe that if you really must make these changes, you can do so by connecting

Computed Column with Current DateTime?

痴心易碎 提交于 2019-12-01 20:41:00
问题 I am trying to create a SQL Server 2008R2 Table column with automatically calculated current datetime for every row I insert. I set Computed Column Specification value for this column equals to GetDate(). My table had some old data in it. After adding GetDate() function, the column get updated with current datetime. But my problem is, When i insert new row in this table, my datetime column never get updated with current date time and this column have same value for each row. Which function

Odd error using IF/ELSE IF statements

雨燕双飞 提交于 2019-12-01 20:11:37
I'm trying to create a temp table dependent on the value of a scenario parameter and using the following IF statement but getting the error below: IF @indexName = 'A' begin select top 400 * into #temp from #pretemp order by EMRev desc end ELSE IF @indexName = 'B' begin select top 75 * into #temp from #pretemp order by EMRev desc end ELSE IF @indexName = 'C' begin select top 300 * into #temp from #pretemp order by EMRev desc end ELSE begin select top 100 * into #temp from #pretemp order by EMRev desc end Msg 2714, Level 16, State 1, Line 179 There is already an object named '#temp' in the

Odd error using IF/ELSE IF statements

£可爱£侵袭症+ 提交于 2019-12-01 19:53:59
问题 I'm trying to create a temp table dependent on the value of a scenario parameter and using the following IF statement but getting the error below: IF @indexName = 'A' begin select top 400 * into #temp from #pretemp order by EMRev desc end ELSE IF @indexName = 'B' begin select top 75 * into #temp from #pretemp order by EMRev desc end ELSE IF @indexName = 'C' begin select top 300 * into #temp from #pretemp order by EMRev desc end ELSE begin select top 100 * into #temp from #pretemp order by

Incorrect syntax near 'PIVOT'

青春壹個敷衍的年華 提交于 2019-12-01 19:16:58
I'm running SQL Server 2008 R2. I'm trying to build a table that takes data from a table structured like this: company | ded_id | descr 10 1 MEDINS 10 2 LIFE 10 3 PENSN ... 10 50 DOMREL And I need to build a temp table it out to a format like this: company | DESC1 | DESC2 | DESC3 ... | DESC50 10 MEDINS LIFE PENSN DOMREL So I built the following query: SELECT * FROM ( SELECT company,'DESC'+CAST(ded_id as VARCHAR(2)) AS DedID,descr FROM deduction ) deds PIVOT (MAX(descr)FOR DedID IN([DESC1],[DESC2],[DESC3])) descs So running this gives the following error: Msg 325, Level 15, State 1, Line 6

Using variable to specify 'size' when declaring a VARBINARY

試著忘記壹切 提交于 2019-12-01 19:12:08
问题 In SQL Server (2008 R2), instead of doing this: DECLARE @testVar VARBINARY(64); I would like to do this: DECLARE @varSize INT; SET @varSize = 64; DECLARE @testVar VARBINARY(@varSize); But I get this error: Incorrect syntax near '@varSize'. How can I do something like this or force SQL to evaluate @varSize? 回答1: For a variable, why don't you just use MAX? DECLARE @testVar VARBINARY(MAX); This isn't the 70s anymore. Your system can handle it. In fact if what you want to do were possible, I

Incorrect syntax near 'PIVOT'

青春壹個敷衍的年華 提交于 2019-12-01 18:36:38
问题 I'm running SQL Server 2008 R2. I'm trying to build a table that takes data from a table structured like this: company | ded_id | descr 10 1 MEDINS 10 2 LIFE 10 3 PENSN ... 10 50 DOMREL And I need to build a temp table it out to a format like this: company | DESC1 | DESC2 | DESC3 ... | DESC50 10 MEDINS LIFE PENSN DOMREL So I built the following query: SELECT * FROM ( SELECT company,'DESC'+CAST(ded_id as VARCHAR(2)) AS DedID,descr FROM deduction ) deds PIVOT (MAX(descr)FOR DedID IN([DESC1],

append currency symbol to result of sql query

北城余情 提交于 2019-12-01 18:23:01
The result of a sql query select PayerDate,PaymentAmount from Payments PaymentAmount - decimal Date Amount 12/11/2012 34.31 12/11/2012 95.60 12/11/2012 34.31 is that possible to get the result of query as below: Date Amount 12/11/2012 $34.31 12/11/2012 $95.60 12/11/2012 $34.31 I have tried but couldn't find much info on this. you can concatenate it on your projection statement, In MySQL, SELECT PayerDate, CONCAT('$', PaymentAmount) PaymentAmount FROM Payments In SQL Server, SELECT PayerDate, '$' + CAST(PaymentAmount AS VARCHAR(15)) PaymentAmount FROM Payments Try this Query select PayerDate,'$