sql-server-2005

pass dynamic file path of excel to “OPENROWSET”

♀尐吖头ヾ 提交于 2019-12-20 07:13:06
问题 I want pass dynamic URL of excel to "OPENROWSET". NOTE - I am passing returned result of excel file to cursor. I want to pass file path to "@excelpath", I have tried many ways but its giving syntax error. ALTER procedure [dbo].[import_excel] ( @excelpath as nvarchar(max) ) as begin set nocount on DECLARE insert_cursor CURSOR FOR select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\memberdata.xlsx', [Sheet1$]) OPEN insert_cursor; FETCH NEXT FROM insert_cursor INTO @id

SQL Server 2005 error when grouping using subquery

好久不见. 提交于 2019-12-20 06:40:02
问题 Using SQL Server 2005 I'm trying to group based on a case statement with a subquery, but I'm getting an error ("Each GROUP BY expression must contain at least one column reference. "). I can work round it quite easily, but can anyone explain the error? I've got a column reference to #header.header. create table #header (header int) create table #detail (header int, detail int) insert into #header values (1) insert into #header values (2) insert into #header values (3) insert into #detail

how i can generate programmatically “insert into” data script file from a database table?

China☆狼群 提交于 2019-12-20 06:37:45
问题 is there an elegant object-orient based framework? 回答1: Here is some code that I wrote for generating 'insert' stored procedures for every table in a database. It also handles returning the new id for those tables that have an identity column. It uses SQL SMO. Some of it is a bit specific to my project so please let me know if you have any questions. void InsertScripts(Database db) { var tables = db.Tables.ToIEnumerable(); //this is an extension method to convert Database.Tables into an

SQL Server 2005: Importing data from SQL Server 2000

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 06:28:17
问题 In SQL Server 2000, you have the "All Tasks... - Export Data" option. Where is this option the SQL Server 2005 Management Studio? Or, is there a SQL Server 2005 way of doing this? EDIT: I am using the Express edition. EDIT: Joel's response answers my question but Mike's answer gives a great alternative to those of us using the Express edition (vote him up!!). 回答1: If you're using the express edition of management studio the Import and Export features aren't available. 回答2: You could use the

SQL ORDER BY months starting with this month

荒凉一梦 提交于 2019-12-20 06:12:27
问题 I have a query that returns months 1-12. (INT) Is there a way to order the results starting on this month desc? Example 3 4 5 6 7 8 9 10 11 12 1 2 Thanks! EDIT: By this month desc, I mean this actual month. MONTH(GetDate()) 回答1: Try simple maths in order by, if month is smaller then current add 12, not sure what will be the performance impact ... Order By Case When month(dateColumn) < month(GetDate()) Then month(dateColumn) +12 ELSE month(dateColumn) END 回答2: You can add a year in there for

How to return 1 single row data from 2 different tables with dynamic contents in sql

◇◆丶佛笑我妖孽 提交于 2019-12-20 05:55:46
问题 Can someone provide answer to this situation?? Suppose I have 2 tables: Table Books with values Batch_no and Title Batch_no - Title 1 - A 2 - B and; Table Book_Authors with values Batch_no and Author_no Batch_no - Author_no 1 - 1 1 - 2 1 - 3 2 - 1 How should I merge the values into 1 row which should look like this Batch_no Author 1 - 1, 2, 3 2 - 1 Any help will be greatly appreciated...Many Thanks! 回答1: If you take a look here: http://www.simple-talk.com/sql/t-sql-programming/concatenating

ROW_NUMBER Alternative for SQL Server 2000

。_饼干妹妹 提交于 2019-12-20 05:31:47
问题 RIGHT now I'm using ROW_NUMBER() in my procedure in SQL Server 2008 as follows: WITH cars as(SELECT carid,mileage,retailprice,imageurl,model,year, Zips.Distance AS Miles, Manufacturers.mfgName as Make, dealers.companyname as companyname, CASE @sortby WHEN 'D' THEN ROW_NUMBER() OVER (ORDER BY Manufacturers.mfgName) WHEN 'P' THEN ROW_NUMBER() OVER (ORDER BY retailprice) WHEN 'M' THEN ROW_NUMBER() OVER (ORDER BY mileage) END as 'rownum' FROM usedcars INNER JOIN #TempZips Zips ON Zips.ZipCode

sql query takes more time when run in a view

一个人想着一个人 提交于 2019-12-20 05:15:10
问题 HI all, I have a huge sql query. When i put that query in a stored Proc it takes 5 seconds to execute which i run it just as a query it takes 4-5 seconds but when i run it in a view it takes 5 mins. Please advise why its running that slow in a view sql query is below: CREATE VIEW dbo.Client_Billing_RS AS SELECT DISTINCT TOP (100) PERCENT CLIENT.OH_Code AS CLIENT, BUYER.OH_Code AS BUYER, dbo.Client_ReturnWK(pallet.MB_PR_CLOSED_DT) AS WEEKNUM, dbo.Client_PadString(DATEPART(MONTH, CONVERT

Activity Monitor Problems in SQL Server 2005

大兔子大兔子 提交于 2019-12-20 04:39:08
问题 I am looking at the Activty Monitor for SQL Server 2005 and we have some processes that are taking up large amounts of the CPU. When I look at what is trying to be run I get: set transaction isolation level read committed This code is not coming from any of our applications. What is causing it? What should be done? 回答1: Look at sys.dm_exec_sessions and sys.dm_exec_connections for the session ids that take up CPU. You'll find the application name, host name and process id of the client. 回答2:

Select records from start of month to current date

↘锁芯ラ 提交于 2019-12-20 04:36:44
问题 I'm trying to select records that were added to the database between the start of the current month and the current day - I more or less know how to get records from the current day, and within a specific time period - but how do I get it so it starts from the beginning of the current calendar month? 回答1: DECLARE @sm DATETIME; SET @sm = DATEADD(DAY, 1-DAY(GETDATE()), DATEDIFF(DAY, 0, GETDATE())); SELECT columns FROM dbo.foo WHERE datetime_column >= @sm; 回答2: WHERE YEAR([Date])=YEAR(GETDATE())