Stop Using Dynamic Sql

Jun 9, 2006

Hi,
The following stored procedure (SP) is using a dynamic sql to build the query.
How can this be written using a standard sql. i.e. NOT dynamically being built.
Initially I thought I can have something like the following query but it does not seem to be working for when the where caluse parameters are not passed.
So I ended up using the dynamic sql as it returns the correct data.
Can you see how the SP can be altered please?
Thanks

------------------------this query does not return the correct data where the where parameters are not passed.------------
select
*
from
tbl_Management
where

([Year] is null or [Year] = @Year)
AND
(YearPeriod is null or YearPeriod = @YearPeriod)
AND
(AreaCode is null or AreaCode = @AreaCode)

---------------------------------------------------------------


create procedure usp_PatientManagementAdminGet

@Year int = null,
@YearPeriod int = null,
@AreaCode varchar(3) = null

as

declare @sql varchar(1000)

set @sql = 'select'
set @sql = @sql + ' ID'
set @sql = @sql + ' ,AreaCode'
set @sql = @sql + ' ,Year'
set @sql = @sql + ' ,YearPeriod'
set @sql = @sql + ' ,A1=A2+A3'
set @sql = @sql + ' ,A2,A3'
set @sql = @sql + ' ,B1=B2+B3'
set @sql = @sql + ' ,X1=convert(int, ((B2+B3)*1.0/(A2+A3))*100)'
set @sql = @sql + ' from'
set @sql = @sql + ' tbl_Management'
set @sql = @sql + ' where'

if (@Year > 0)
begin
set @sql = @sql + ' [Year] = ' + convert(varchar(4), @Year)
set @sql = @sql + ' AND'
end
if (@YearPeriod > 0)
begin
set @sql = @sql + ' YearPeriod = ' + convert(varchar(2), @YearPeriod)
set @sql = @sql + ' AND'
end
if (@ProgrammeAreaCode is not null)
begin
set @sql = @sql + ' AreaCode = ''' + convert(varchar(3), @AreaCode) + ''''
set @sql = @sql + ' AND'
end

--trim off the last AND...
set @sql = left(@sql, len(@sql) - 3)

exec sp_sqlexec @sql

View 3 Replies


ADVERTISEMENT

Design Patterns Research - Dynamic Error Count Manipulation To Determine On What Type Of Error To Stop Job

Jan 31, 2008



I would like to fail a package depending on the error. The package extracts data from Excel files. I would like to continue processing if an Excel file is badly formatted, but stop processing if there is a serious issue. like the file server hosting the Excel files crashed.
I was thinking about dynamically changing the MaxeErrorCount property based on the Error ID or description.


Any ideas on an intelligent/simple way to do this

View 1 Replies View Related

Importing Excel Sheet Which Have Dynamic Column Name And Dynamic Number Of Columns

Aug 25, 2007

Hi Craig/Kamal,

I got your email address from your web cast. I really enjoyed the web cast and found it to be
very informative.

Our company is planning to use SSIS (VS 2005 / SQL Server 2005). I have a quick question
regarding the product. I have looked for the information on the web, but was not able to find
relevant information.

We are getting Source data from two of our client in the form of Excel Sheet. These Excel sheets
Are generated using reporting services. On examining the excel sheet, I found out that the name
Of the columns contain data itself, so the names are not static such as Jan 2007 Sales, Feb 2007 Sales etc etc.
And even the number of columns are not static. It depends upon the range of date selected by the user.

I wanted to know, if there is a way to import Excel sheet using Integration Services by defining the position
Of column, instead of column name and I am not sure if there is a way for me to import excel with dynamic
Number of columns.

Your help in this respect is highly appreciated!

Thanks,


Hi Anthony, I am glad the Web cast was helpful.

Kamal and I have both moved on to other teams in MSFT and I am a little rusty in that area, though in general dynamic numbers of columns in any format is always tricky. I am just assuming its not feasible for you to try and get the source for SSIS a little closer to home, e.g. rather than using Excel output from Reporting Services, use the same/some form of the query/data source that RS is using.

I suggest you post a question on the SSIS forum on MSDN and you should get some good answers.
http://forums.microsoft.com/msdn/showforum.aspx?forumid=80&siteid=1
http://forums.microsoft.com/msdn/showforum.aspx?forumid=80&siteid=1

Thanks



Craig Guyer
SQL Server Reporting Services

View 12 Replies View Related

SSRS 2005 - Email Report On Execution To Dynamic List With Dynamic Parameters = No Schedule

Nov 23, 2007

Hi,
I have a need to display on screen AND email a pdf report to email addresses specified at run time, executing the report with a parameter specified by the user. I have looked into data driven subscriptions, but it seems this is based on scheduling. Unfortunately for the majority of the project I will only have access to SQL 2005 Standard Edition (Production system is Enterprise), so I cannot investigate thoroughly.

So, is this possible using data driven subscriptions? Scenario is:

1. User enters parameter used for query, as well as email addresses.
2. Report is generated and displayed on screen.
3. Report is emailed to addresses specified by user.

Any tips on how to get this working?

Thanks

Mark Smith

View 3 Replies View Related

Merge Replication W/ Dynamic Row Filter - Not 'dynamic' After First Initial Sync?

May 2, 2007

If anyone could confirm...

SQL Server 2000 SP4 to multiple SQL Server 2005 Mobile Edition on PDAs. My DB on SQL2k is published with a single dynamic row filter using host_name() on my 'parent' table and also join filters from parent to child tables. The row filter uses joins to other tables elsewhere that are not published to evaluate what data is allowed through the filter.

E.g. Published parent table that contains suppliers names, etc. while child table is suppliers' products. The filter queries host_name(s) linked to suppliers in unpublished table elsewhere.

First initial sync with snapshot is correct and as I expected - PDA receives only the data from parent (and thus child tables) that matches the row filter for the host_name provided.

However - in my scenario host_name <--> suppliers may later be updated E.g. more suppliers assigned to a PDA for use or vice versa. But when I merge the mobile DB, the new data is not downloaded? Tried re-running snapshot, etc., no change.

Question: I thought the filters would remain dynamic and be applied on each sync?

I run a 'harmless' update on parent table using TSQL e.g. "update table set 'X' = 'X'" and re-sync. Now the new parent records are downloaded - but the child records are not!

Question: I wonder why if parent records are supplied, why not child records?

If I delete existing DB and sync new, I get the updated snapshot and all is well - until more data added back at server...

Any help would be greatly appreciated. Is it possible (or not) to have dynamic filters run during second or subsequent merge?

View 4 Replies View Related

T-SQL (SS2K8) :: How To Add Inline TVF With Dynamic Columns From CRL Dynamic Pivot

Mar 9, 2015

I have tried building an Inline TVF, as I assume this is how it would be used on the DB; however, I am receiving the following error on my code, I must be missing a step somewhere, as I've never done this before. I'm lost on how to implement this clr function on my db?

Error:
Msg 156, Level 15, State 1, Procedure clrDynamicPivot, Line 18
Incorrect syntax near the keyword 'external'.
CREATE FUNCTION clrDynamicPivot
(
-- Add the parameters for the function here
@query nvarchar(4000),
@pivotColumn nvarchar(4000),

[code]....

View 1 Replies View Related

Mixing Dynamic SQL With Non-Dynamic In Stored Proc

Mar 24, 2007

I have a Stored Procedure for processing a Bill of Material.

One column on the Assembly Table is a Function Name that contains some busniess rules.

OK, now I'm doing a Proof of Concept and I'm stumped.

Huuuuh!

I will ultimately have about 100 of these things. My plan was using Dynamic SQL to go execute the function.

Note: The function just returns a bit.

So; here's what I had in mind ...

if isnull(@FnNameYN,'') <> ''
exec spinb_CheckYN @FnNameYN, @InvLineID, @FnBit = @FnBit output




CREATE PROCEDURE dbo.spinb_CheckYN
@FnNameYN varchar(50),
@InvLineID int,
@FnBit bit output
AS

declare @SQL varchar(8000)

set @SQL = '
if dbo.' + @FnNameYN + ' (' + convert(varchar(31),@InvLineID) + ')) = 1
set @FnBit = 1
else
set @FnBit = 0'

exec (@SQL)
GO



Obviously; @FnBit is not defined in @SQL so that execution will not work.
Server: Msg 137, Level 15, State 1, Line 4
Must declare the variable '@FnBit'.
Server: Msg 137, Level 15, State 1, Line 5
Must declare the variable '@FnBit'.


So; is there a way to get a value out of a Dynamic SQL piece of code and get that value INTO my OUTPUT variable?


My many thanks to anyone who can solve this riddle for me.
Thank You!


Sigh: For now, it looks like I'll have a huge string of "IF" statements for each business rule function, as follows:
Hopefully a better solution comes to light.

------ Vertical Build1 - Std Vanes -----------
if @FnNameYN = 'fnb_YN_B1_14'
BEGIN
if dbo.fnb_YN_B1_14 (convert(varchar(31),@InvLineID) ) = 1
set @FnBit = 1
else
set @FnBit = 0
END

------ Vertical Build1 - Scissor Vanes -----------
if @FnNameYN = 'fnb_YN_B1_15'
BEGIN
if dbo.fnb_YN_B1_15 (convert(varchar(31),@InvLineID) ) = 1
set @FnBit = 1
else
set @FnBit = 0
END
.
.
.
etc.

View 10 Replies View Related

Net Stop....

Mar 11, 2002

Hi All,
I'd like to remotly stop an instance of SQL Server
but this command does not seem to work....

net stop mssql$<server name> (eg. net stop mssql$prod)

...any ideas? I'll want to start this sql server again too.

David.

View 1 Replies View Related

Stop SQL CLR In DB

Jan 30, 2008

Is there any way to stop SQL CLR on database apart from using

sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO

I want to stop this functionality on database...any idea...

View 1 Replies View Related

SQL Autoincrement Id STOP

Apr 10, 2008

 Hi  All        I am using SQL server Database in one of my table there is a column which is set to Identity=Yes i.e., The ID is increment by one on every insert and if  the insertion failed then the id generated goes off then in the next generation it uses new id ..........EXfirst insertion id=1 then in the second insertion if while adding data to other rows if i get some error  then the id 2 is not used and when i correct the error and insert it then id=3? can any one give me the solution for this and NextWhen i delete the datafrom the table see the ids are upto 20 and i delete all the records from the table after insertion of new  record the id will be 21plese help me in this  

View 3 Replies View Related

How Do I Stop These Duplicates

Feb 25, 2004

I have these two tables and I cant prevent duplicates.
SEARCH
Item
ItemID
Info

CATEGORYDATA
CategoryID
ItemID

SELECT DISTINCT SEARCH.ItemId, SEARCH.Item, CATEGORYDATA.CategoryId
FROM SEARCH
INNER JOIN CATEGORYDATA
ON SEARCH.ItemID = CATEGORYDATA.ItemID
And I get something like:ItemID Item CategoryID
1 item1 1
3 item3 1
1 item1 2 <---duplicate
1 item1 3 <---duplicate
2 item2 3
4 item4 3
Thanks in advance

View 4 Replies View Related

Stop Replication

Apr 29, 2002

I have a snapshot replication is running and now I want to stop the replication for a while. Is it possible to do that? If it is then where I can set to stop it? Please help.

Thanks for Help!

View 2 Replies View Related

Net Use Start/stop Sql

Apr 25, 2000

Hello,

Does any one know how I can start and stop the sql services with the net use command?

Thanks,
Anthony

View 1 Replies View Related

Cant Stop SQL Server

Sep 1, 1998

I use EM to handle 2 SQL servers. One I can `stop`; the other I can`t. (except I think I used to be able to do so).

When I select the `stop` I get the following message from EM:

"An error 1051 - (A stop control has bee sent to a service which other running services are dependent on) occurred while performing the service operation on the MSSQLServer service."

How do I track down what this other running service is? How do I stop SQL?

All help greatly appreciated.

Judith

View 1 Replies View Related

Start/Stop SQL From Far Away

May 10, 2002

I need to remotely start and stop SQL from another machine from a program or command line. Thanks in advance.

View 1 Replies View Related

How To Stop Replication

Aug 29, 2002

I wanted to remove my Northwind database. But that database is currently used for replication. I'll have to stop the replication first before I can remove it.

So how to stop the replication?

Thanks!

amy

View 1 Replies View Related

Stop Execution

Feb 13, 2006

Hi,
Can anybody tell me how to stop the execution of a T-SQL statement at once? I have tried Alt+Break but its taking a long time to stop.Whats the reason?Plz suggest....I am dealing with a database containing 24343000 data.
Joydeep

View 5 Replies View Related

Why Would This Stop Working

Sep 7, 2007

Hey

I have a statement that has been running great for the past hour but now it will not pull the info any longer and just gives me a null

DECLARE @Text VARCHAR(2000)
SELECT @Text = COALESCE(@Text + '', '') + x.memotext
FROM (SELECT TOP 100 PERCENT memotext FROM customermemoheader WHERE memonumber = 'TERMS' and customernumber = '0009' ORDER BY seqnumber) AS x

SELECT @Text AS MemoText


I have verified in the tables that the info is there by running the select statement from within the (). It has worked for 8000 records and now it no longer works. Any help would be much appreciated.

THanks

View 14 Replies View Related

Stop Preparing My SQL!

Nov 30, 2007

I have a performance issue with a Cognos report against SQL Server 2005.
The total running time of the report is 1 minute 15, and using SQL Profiler I found out that 1m13 is spent preparing SQL. Execution and generating the report takes up 2 seconds; no problem there.
The SQL is the same every time I run the report, yet SQL Server spends 1m13 preparing it every single time! I'm no DBA, but as far as I understand that's not what's supposed to happen; once prepared, the SQL should execute quickly every time.

Is there a way to stop SQL from preparing the statement every time?

(Cognos 8 against SQL Server 2005 through OLEDB. Oh, and this query takes about a second when run in EM.)

View 14 Replies View Related

How To Stop Replication

Jun 29, 2015

How do you stop Replication?It is causing blocking. URLs....

View 9 Replies View Related

Do We Have A STOP Command

Feb 26, 2008

Hey Guys, I have a question. I know you can put a 'STOP' command in VBA code. Is there something like that for SQL Server Stored Procedures?

View 3 Replies View Related

How To Stop Logging

Jul 20, 2005

Hi,I am doing some resource hungry tasks (some extraction and loadingthrough DTS), for which each time the SQL Server Log files gets filledup!Is there any way to stop the logging (like as during restore)?Thanks in advance.-surajit

View 1 Replies View Related

How To Stop Mssql

Jul 20, 2005

Hi.I need to stop mssql service via command row.How can I do?Tnk,Luca

View 1 Replies View Related

My Database Will Be Stop ????

Oct 31, 2007



Hello,

i'm desperate! I have a application in my SQL Express and only one database for it. But right now it have 3,97gb, without log file.
i'm already buying a SQL Enterprise to upgrade this, take some days, but my question is: when this size came to 4,00gb my application will be stop working ? my business stop ???

Thanks

View 1 Replies View Related

There Must Be A Way To Tell SSIS Don't Stop; Keep Going

Apr 22, 2008



Here are some production error messages:


Error: 0xC0047039 at Load work$$MyDataFile from flat file, DTS.Pipeline:
SSIS Error Code DTS_E_THREADCANCELLED.
Thread "WorkThread0" received a shutdown signal and is terminating.
The user requested a shutdown, or an error in another thread is causing the pipeline to shutdown.
There may be error messages posted before this with more information on why the thread was cancelled.

Error: 0xC0047021 at Load work$$MyDataFile from flat file, DTS.Pipeline:
SSIS Error Code DTS_E_THREADFAILED. Thread "WorkThread0" has exited with error code 0xC0047039.
There may be error messages posted before this with more information on why the thread has exited.

I did not request a shutdown!
Someone please give me a global switch to tell SSIS to keep going.
Why should I not load 20,00 records because of one bad record?


I have tediously changed every field on every tool to "Ignore Error".
Why should I still have to do this? I mean why does SSIS default to failure?
Is there some switch I could change in the XML that would change this default behaviour?

The problem with the data in this case: a NewLine character was missing so it looked to the data flow as one very long line.

Hoping for a solution,
IanO

View 1 Replies View Related

How To Stop A Package

May 26, 2006

Hi,

In one of my packages, I have a script component to do a transformation and I am inserting the output to table. Within that script component, if the data does not match my requirement, I have to stop the job. For that, I am using 'componentmetadata.fireError' to raise an error. The problem is, now even when an error is raised, the job completes by loading the remaining records and then aborts. But I want to abort the job as soon it raises an error. How to do this?

Thanks.

View 8 Replies View Related

Synchronization Stop At 0%

Aug 6, 2007

I create a publication with 1 article with a parameterized filter. Empty table without records. Then i went to create a snapshot through snapshot agent. After snapshot was created, data are inserted with "Insert Into..Select" SQL statement. A total of 20 millions records (10GB) are inserted. When i try to sync with a SQL compact edition, the synchronization % stop at 0 for more than 1 hour. Does that mean it is not working?

Thank you and have a nice day.

View 1 Replies View Related

Dynamic Cursor/ Dynamic SQL Statement

Oct 24, 2004

I've looked up Books Online on Dynamic Cursor/ Dynamic SQL Statement.

Using the examples given in Books Online returns compilation errors. See below.

Does anyone know how to use Dynamic Cursor/ Dynamic SQL Statement?

James



-- SQL ---------------

EXEC SQL BEGIN DECLARE SECTION;
char szCommand[] = "SELECT au_fname FROM authors WHERE au_lname = ?";
char szLastName[] = "White";
char szFirstName[30];
EXEC SQL END DECLARE SECTION;

EXEC SQL
DECLARE author_cursor CURSOR FOR select_statement;

EXEC SQL
PREPARE select_statement FROM :szCommand;

EXEC SQL OPEN author_cursor USING :szLastName;
EXEC SQL FETCH author_cursor INTO :szFirstName;



--Error--------------------
Server: Msg 170, Level 15, State 1, Line 23
Line 23: Incorrect syntax near ';'.
Server: Msg 1038, Level 15, State 1, Line 24
Cannot use empty object or column names. Use a single space if necessary.
Server: Msg 1038, Level 15, State 1, Line 25
Cannot use empty object or column names. Use a single space if necessary.
Server: Msg 170, Level 15, State 1, Line 27
Line 27: Incorrect syntax near ';'.
Server: Msg 170, Level 15, State 1, Line 30
Line 30: Incorrect syntax near 'select_statement'.
Server: Msg 170, Level 15, State 1, Line 33
Line 33: Incorrect syntax near 'select_statement'.
Server: Msg 102, Level 15, State 1, Line 35
Incorrect syntax near 'author_cursor'.
Server: Msg 170, Level 15, State 1, Line 36
Line 36: Incorrect syntax near ':'.

View 2 Replies View Related

Dynamic Source And Dynamic Destination

Apr 15, 2008

I have a requirment which i have partly accomplished , but could not get through completely

i have a file which comes in a standard format ending with date and seq number ,

suppose , the file name is abc_yyyymmdd_01 , for first copy , if it is copied more then once the sequence number changes to 02 and 03 and keep going on .

then i need to transform those in to new file comma delimited destination file with a name abc_yyyymmdd,txt and others counting file counting record abc_count_yyyymmdd.txt. and move it to a designated folder. and the source file is then moved to archived folder


what i have taken apprach is

script task select source file --------------------> data flow task------------------------------------------> script task to destination file

dataflow task -------------------------> does count and copy in delimited format



what is happening here is i can accomlish a regular source file convert it to delimited destination file --------> and move it to destination folder with script task .

but cannot work the dynamic pick of a source file.


please advise with your comments or solution you have

View 14 Replies View Related

Stop Automated Restore

Aug 3, 2006

We have automated the process of backing up and restoring the databases. But the problem is One database is restoring from several hrs ..though it is supposed to take only a few mins. So there is some issue with it and I want to find out how I can stop the restoring process.
Its not a command am running in query analyser or Enterprise manager, but its automated so i cant see the command to stop it.
 Any ideas?

View 2 Replies View Related

What's The Best Method To Stop SQL Injection?

Nov 20, 2006

Does UrlEncode have any impact on SQL injection?  How would I go about protecting my site?

View 3 Replies View Related

What Is SqlInjection And How To Resolve/stop It?

Feb 14, 2007

Hi all,
I found some articles online regarding sql injection, but not clear. Can someone give me few examples how to avoid it.
 
Thanks
Sham

View 2 Replies View Related

How Can I Stop The Totals From Multiplying

Aug 20, 2007

I have a db that tracks clients, payments, and clients codes. A client can be assigned many codes. In my query I'm asking to see any clients that have two selected codes and also of those show me those that have a sum of payments between 1 and 100000 dollars. The query returnes the clients with the selected codes, but the total payments is multiplied by the number of the codes they have of the ones that I select. In this case 'email group' or 'member'
So if a client had a total of $20,000 dollars in payments and they had both of the codes I selected the sum of payments is returned as $40,000.
Any thoughts are appreciated!
Here is a sample example:
SELECT DISTINCT [Salutation], [Client_ID] AS 'Client ID', SUM([dbo].[vw_ClientTotals].[Total Payments]) AS 'Sum(Total Payments)'FROM [dbo].[All_Constituents]INNER JOIN [dbo].[tblClientCodes] ON [dbo].[tblClientCodes].[Client_ID]=[dbo].[All_Constituents].[Client_ID]INNER JOIN [dbo].[tblClientCodeLookUp] ON [dbo].[tblClientCodeLookUp].[ClientCode_ID]=[dbo].[tblClientCodes].[ClientCodeLookup_ID]INNER JOIN [dbo].[vw_ClientGiftTotals] ON [dbo].[vw_ClientGiftTotals].[Client_ID]=[dbo].[All_Constituents].[Client_ID]WHERE ( ([dbo].[tblClientCodeLookUp].[ClientCode] = 'Member') OR ([dbo].[tblClientCodeLookUp].[ClientCode] = 'Email Groups') )GROUP BY [dbo].[All_Constituents].[Salutation], [dbo].[All_Constituents].[Client_ID]HAVING  (SUM([dbo].[vw_ClientTotals].[TotalPayments]) BETWEEN '1' AND '100000') 

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved