sql-server-2008

row convert to column in sql 2008

╄→尐↘猪︶ㄣ 提交于 2020-01-06 07:12:56
问题 I want to convert a series of rows into a series of columns create table #cusphone(cusid int,cusph1 int) insert into #cusphone values(1,48509) insert into #cusphone values(1,48508) insert into #cusphone values(1,48507) insert into #cusphone values(2,48100) so that the output is like this 1 48509 48508 48507 2 48100 null null 回答1: You can use the same approach of rank() and then use the new PIVOT function as follows: with cusCte as( select cusid,cusph1,RANK() over (partition by cusid order by

row convert to column in sql 2008

杀马特。学长 韩版系。学妹 提交于 2020-01-06 07:12:13
问题 I want to convert a series of rows into a series of columns create table #cusphone(cusid int,cusph1 int) insert into #cusphone values(1,48509) insert into #cusphone values(1,48508) insert into #cusphone values(1,48507) insert into #cusphone values(2,48100) so that the output is like this 1 48509 48508 48507 2 48100 null null 回答1: You can use the same approach of rank() and then use the new PIVOT function as follows: with cusCte as( select cusid,cusph1,RANK() over (partition by cusid order by

Users and Staff in the same table or separate tables?

感情迁移 提交于 2020-01-06 06:50:27
问题 I have to decide on design of my database for my application. So far I have Users table that stores these fields: **USERS** RecID - auto increment UserID - unique id (Primary key) UserName Password Salt TempPassword FirstName LastName Email SystemAdmin QuestionID Answer Active CreateDate CreateUID This table stores all information about users. There is another section in the app named Staff. According to the specs they want to see on the form **STAFF** First name Last name Middle Initial

column name or number … not match table definition in table created by create script.Error displayed while inserting rows from one table to another

孤街醉人 提交于 2020-01-06 06:41:18
问题 I have two identical tables in different server instances. One server is production and the other one is for testing. The testing tables where created by using scripts created by SQL management studio (Right click on table -->script table as --> Create). To move test data i am using a linked server and the following code : set identity_insert <Server>.<DB>.<schema>.<SomeID> ON insert into <Server>.<DB>.<schema>.<TestTb> select top 100 * from <Server>.<DB>.<schema>.<ProdTB> set identity_insert

SQL Server MERGE not always creating a row for a member_ email_ read table

微笑、不失礼 提交于 2020-01-06 06:41:10
问题 This is a continuation to the question posted - Stored Procedure/SQL script that allows me to add a record to a table I have 2 tables, one with all the email data, and another with all the specific member email data that one creates a row if an email has been read. Once an email has been read by a member its added to the Member_email_read table (which is created and populated based on all read emails). The tables are as follows: SELECT [member_email_id] ,[member_email_FK_message_id] ,[member

XML Parsing - Illegal XML Character (when executing stored procedure, running procedure queries results in no errors)

大憨熊 提交于 2020-01-06 06:16:12
问题 I have a valid XML document (this has been confirmed using multiple XML validators including online validators and the Sublime Text XML validator plugin). I receive the following error when attempting to import the XML document into MSSQL 2008 using a stored procedure named ImportNXML (command: exec [dbo].[ImportNXML]; ) Msg 9420, Level 16, State 1, Line 2 XML parsing: line 17, character 35, illegal xml character I have confirmed no illegal characters are in the XML document and line 17,

t-sql string concatenation

痞子三分冷 提交于 2020-01-06 06:11:24
问题 i have a table that has following column Type -------- type 1 type 2 type 3 How can i convert the above to a string like ('type 1', 'type 2', 'type 3') I want to use the output in my t-sql query with IN clause. Something like select * from TableA where SomeColumn IN ('Type 1','Type 2', Type 3') I used to following to come up with output (type 1, type 2, type 3) select '(' + STUFF((select ', ' + Type from TableA for xml path ('')),1,2,'') + ')' But dont know how to insert the single quotes.

t-sql string concatenation

寵の児 提交于 2020-01-06 06:11:13
问题 i have a table that has following column Type -------- type 1 type 2 type 3 How can i convert the above to a string like ('type 1', 'type 2', 'type 3') I want to use the output in my t-sql query with IN clause. Something like select * from TableA where SomeColumn IN ('Type 1','Type 2', Type 3') I used to following to come up with output (type 1, type 2, type 3) select '(' + STUFF((select ', ' + Type from TableA for xml path ('')),1,2,'') + ')' But dont know how to insert the single quotes.

Query pulling 12-15 GB data From more than 120 tables

半腔热情 提交于 2020-01-06 05:40:20
问题 I have a query which is pulling data from almost 125 different tables, I have created some 13 nested Stored Procedures calling other stored procedures to pull all the required data. Surprise Surprise Surprise The query takes ages to execute and sometimes I have to kill he connection and rerun it. I have been advised to make use of staging table, Move required data there using SSIS packages and pull data from there, but I am a bit reluctant to use SSIS as Im not very comfortable with SSIS and

Using crypt_gen_random to generate unique Serial Numbers

眉间皱痕 提交于 2020-01-06 05:30:07
问题 Trying to generate unique serial numbers with alphanumerics (A-Z,a-z,0-9) and no special characters.Used below code where sLength was min 10 and max 12 as defined in front end. declare @sLength tinyint declare @randomString varchar(50) declare @counter tinyint declare @nextChar char(1) declare @rnd as float set @sLength = 3 set @counter = 1 set @randomString = '' while @counter <= @sLength begin -- crypt_gen_random produces a random number. We need a random -- float. select @rnd = cast(cast