sql-server-2008-r2

Difference of create Index by using include column or not using

丶灬走出姿态 提交于 2019-11-29 16:28:35
问题 I Want To Create Index In SQL Server 2008 R2 in Column1 and Column2 What is the difference of below query: Not include CREATE NONCLUSTERED INDEX [IX_1] ON [dbo].[MyTable] ( [Column1] ASC, [Column2] ASC ) ON [PRIMARY] Or include: CREATE NONCLUSTERED INDEX [IX_2] ON [dbo].[MyTable] ( [Column1] ASC ) INCLUDE ([Column2]) ON [PRIMARY] 回答1: In the first one Column2 gets added to the index key. In the second one it might not (*) get added to the key in which case it will only appear in the index

SSIS how to convert string (DT_STR) to money (DT_CY) when source has more than 2 decimals

孤人 提交于 2019-11-29 16:05:26
I have a source flat file with values such as 24.209991, but they need to load to SQL Server as type money. In the DTS (which I am converting from), that value comes across as 24.21. How do I convert that field in SSIS? Right now, I am just changing the type from DT_STR to DT_CY, and it gives a run error of 'Data conversion failed. The data conversion for column "Col003" returned status value 2 and status text "The value could not be converted because of a potential loss of data.".' Do I use a Data Conversion task? And then what? I've also tried setting the source output column to DT_NUMERIC,

xp_regread() returned error 5, 'Access is denied.'

感情迁移 提交于 2019-11-29 16:02:29
I'm running the SQL Server Copy Database Wizard. Of note is that the Operator is NT AUTHORITY\SYSTEM , which I thought should have the authority to run whatever it wants. How can we grant sufficient privileges to NT AUTHORITY\SYSTEM ? I have already tried: GRANT EXECUTE ON xp_regread TO public GRANT EXECUTE ON xp_regread TO [NT AUTHORITY\SYSTEM] And running the following shows that it worked. SELECT grantee_principal.name AS [Grantee] , prmssn.permission_name FROM sys.all_objects AS xproc INNER JOIN sys.database_permissions AS prmssn ON prmssn.major_id=xproc.object_id AND prmssn.minor_id=0 AND

How to add dynamic column to an existing table

我是研究僧i 提交于 2019-11-29 16:00:35
I have 2 tables 1st table contains following columns, id code Name 1 c1 chk1 2 c2 chk2 3 c3 chk3 2nd table contains following columns, id,Name,Chk1,chk2,Chk3 i have to add the column 'Chk4' into table2 if table1 is updated with value '4,'c4','ch4' dynamically.How to write procedure to perform this? i've tried the following procedure but its not working fine. create proc Add_Check as begin declare @Column varchar(50) declare @query varchar(255) declare @query1 varchar(255) set @Column= (select top 1 QUOTENAME(Name) from table1 where id=(Select MAX id) from table1)) if exists(select 1 from

SQL Query: How can I get data of row with number 1000 direclty?

谁说我不能喝 提交于 2019-11-29 15:51:02
问题 If I have a SQL Table called Persons that contain about 30000 rows and I want to make a SQL query that retrieve the data of row number 1000 ... I got it by non professional way by making the following query Select Top 1 * from ( Select top 1000 * From Persons Order By ID )A Order By A.ID desc But I feel that's a more optimized query that can do that ... can any lead me to perfect query ? Note : table contain PK column called "ID" but it's not sequential 回答1: row_number is the best approach

SQL Server 2008 R2 using PIVOT with varchar columns not working

妖精的绣舞 提交于 2019-11-29 15:32:34
I'm using SQL Server 2008 R2, I have this simple table What I was trying to do is make a selection from this table and get this following result x | 1 | 2 | 3 --+------------+-------------+------------ 1 | first 1 | first 2 | first 3 2 | Second 1 | second 2 | second 3 I thought that can be done with PIVOT I don't know much about PIVOT AND all my search result found using PIVOT with Count() . SUM() , AVG() which will not work in my table since I'm trying to PIVOT on a varchar column Question am I using the right function? Or is there something else I need to know to solve this issue? Any help

Fill In The Date Gaps With Date Table

自闭症网瘾萝莉.ら 提交于 2019-11-29 15:18:15
I have two tables. An orders table with customer, and date. A date dimension table from a data warehouse. The orders table does not contain activity for every date in a given month, but I need to return a result set that fills in the gaps with date and customer. For Example, I need this: Customer Date =============================== Cust1 1/15/2012 Cust1 1/18/2012 Cust2 1/5/2012 Cust2 1/8/2012 To look like this: Customer Date ============================ Cust1 1/15/2012 Cust1 1/16/2012 Cust1 1/17/2012 Cust1 1/18/2012 Cust2 1/5/2012 Cust2 1/6/2012 Cust2 1/7/2012 Cust2 1/8/2012 This seems like a

TSQLQuery only streams first 1MB of data correctly for large strings

亡梦爱人 提交于 2019-11-29 15:16:14
(see Edit #1 with stack trace and Edit #2 with workaround at end of post) While troubleshooting TSQLQuery.FieldByName().AsString -> TStringStream Corrupts Data , I found that a TSQLQuery.FieldByName().AsBytes will only stream exactly 1MB of varchar(max) data correctly. Using WireShark, I verified that the data is all being handed to the Delphi app correctly . I verified that it always writes out the correct number of bytes to the output file, but any bytes that exceed exactly 1MB are null bytes. Additionally, TSQLQuery.FieldByName().AsString and .AsWideString also exhibit the same behavior.

Create SQL Server table programmatically

随声附和 提交于 2019-11-29 14:38:59
I need to programmatically create a SQL Server 2008 table in C# such that the columns of the table should be generated from a list of columns (each column name is the name of a row in the table) My question is what is the command string to loop through the list of columns and creates the table's recorded: List<string> columnsName = ["col1","col2","col3"] I want to create a table with the columns in the columnsName . But since the list size in not constant, I need to loop through the list to generate the table columns. The simple answer is CREATE TABLE table_name ( column_name1 data_type,

Correct way to take a exclusive lock

半城伤御伤魂 提交于 2019-11-29 14:04:32
I am writing a procedure that will be reconciling finical transactions on a live database. The work I am doing can not be done as a set operation so I am using two nested cursors. I need to take a exclusive lock on the transaction table while I am reconciling per client, but I would like to release the lock and let other people run their queries in between each client I process. I would love to do a exclusive lock on a row level instead of a table level, but what I have read so far says I can not do with (XLOCK, ROWLOCK, HOLDLOCK) if the other transactions are running at READCOMMITED isolation