473,765 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert from sql to postgresql (stored procedure)

1 New Member
I have to convert this script, but i didn't. This is stored procedure of sql. I need to convert to postgresql...


if it is possible please help me!
Thanks
Fatma

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE PROCEDURE usp_generatetable(@prm_tableID int, @prm_langID int)
  3. as 
  4. begin
  5.     declare @table_id int, @field_id int, @match_code varchar(50), @value varchar(250), @first_field int, 
  6.         @query_text varchar(8000), @table_identity varchar(30), @active_match_code varchar(250),
  7.         @dil_id varchar(20), @counter int, @rec_counter int, @data_type varchar(50), @field_name varchar(50)
  8.  
  9.     set nocount on
  10.  
  11.     declare cur_field_list cursor for
  12.         select fieldID from tbl_fields where tableID = @prm_tableID and field_isactive = 1 order by fieldID
  13.  
  14.  
  15.     select @table_identity = 'temp_' + replace(convert(varchar(50), getdate(), 112) + convert(varchar(50), getdate(), 114), ':', '')
  16.  
  17.     set @query_text = 'create table ' + @table_identity + ' (rec_match_code varchar(20), '
  18.  
  19.     set @first_field = 1
  20.  
  21.     open cur_field_list
  22.  
  23.     fetch next from cur_field_list into @field_id
  24.  
  25.     while @@fetch_status = 0
  26.         begin
  27.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  28.  
  29.             if @first_field = 1
  30.                 set @query_text = @query_text + @field_name + ' ' + @data_type
  31.             else
  32.                 set @query_text = @query_text + ', ' + @field_name + ' ' + @data_type
  33.  
  34.             fetch next from cur_field_list into @field_id
  35.  
  36.             set @first_field = 0
  37.         end
  38.  
  39.     set @query_text = @query_text + ' ) '
  40.  
  41.     close cur_field_list
  42.  
  43.     deallocate cur_field_list    
  44.  
  45.     EXEC (@query_text)
  46.  
  47.     declare cur_source_data cursor for
  48.         select fieldID, rec_match_code, rec_value
  49.         from tbl_records
  50.         where tableID = @prm_tableID and langID = @prm_langID
  51.         order by rec_match_code, fieldID
  52.  
  53.     open cur_source_data
  54.     --print @@cursor_rows
  55.  
  56.     set @active_match_code = ''
  57.  
  58.     fetch next from cur_source_data
  59.         into @field_id, @match_code, @value
  60.  
  61.     while @@fetch_status = 0 
  62.         begin
  63.             if @active_match_code <> @match_code
  64.                 begin
  65.                     set @query_text = 'insert into ' + @table_identity + ' (rec_match_code) values ( ' + '''' + @match_code + '''' + ' ) '
  66.  
  67.                     EXEC (@query_text)
  68.  
  69.  
  70.                     set @active_match_code = @match_code                
  71.                 end
  72.  
  73.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  74.  
  75.             set @query_text = 'update ' + @table_identity + ' set ' + @field_name + ' = ' + 'convert(' + @data_type + ', ' + '''' + @value + '''' + ') ' + ' where rec_match_code = ' + '''' + @match_code + ''''
  76.  
  77.             EXEC (@query_text)
  78.  
  79.             fetch next from cur_source_data
  80.                 into @field_id, @match_code, @value
  81.         end
  82.     close cur_source_data
  83.  
  84.     deallocate cur_source_data
  85.  
  86.  
  87.     EXEC('select * from ' + @table_identity)
  88.  
  89.     set @query_text = 'if exists (select * from dbo.sysobjects where name like ' + '''' + '%' + @table_identity + '%' + '''' + ') drop table ' + @table_identity
  90.  
  91.     exec(@query_text) 
  92.  
  93.  
  94.  
  95. end
  96. GO
  97.  
Aug 31 '06 #1
1 8575
doss
2 New Member
Boss,

There are no conceptes absolutly for PROCEDURE in Postgres SQL

I dont think so.

But u can make them as FUNCTIONS in Postgres SQL

I have to convert this script, but i didn't. This is stored procedure of sql. I need to convert to postgresql...


if it is possible please help me!
Thanks
Fatma

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE PROCEDURE usp_generatetable(@prm_tableID int, @prm_langID int)
  3. as 
  4. begin
  5.     declare @table_id int, @field_id int, @match_code varchar(50), @value varchar(250), @first_field int, 
  6.         @query_text varchar(8000), @table_identity varchar(30), @active_match_code varchar(250),
  7.         @dil_id varchar(20), @counter int, @rec_counter int, @data_type varchar(50), @field_name varchar(50)
  8.  
  9.     set nocount on
  10.  
  11.     declare cur_field_list cursor for
  12.         select fieldID from tbl_fields where tableID = @prm_tableID and field_isactive = 1 order by fieldID
  13.  
  14.  
  15.     select @table_identity = 'temp_' + replace(convert(varchar(50), getdate(), 112) + convert(varchar(50), getdate(), 114), ':', '')
  16.  
  17.     set @query_text = 'create table ' + @table_identity + ' (rec_match_code varchar(20), '
  18.  
  19.     set @first_field = 1
  20.  
  21.     open cur_field_list
  22.  
  23.     fetch next from cur_field_list into @field_id
  24.  
  25.     while @@fetch_status = 0
  26.         begin
  27.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  28.  
  29.             if @first_field = 1
  30.                 set @query_text = @query_text + @field_name + ' ' + @data_type
  31.             else
  32.                 set @query_text = @query_text + ', ' + @field_name + ' ' + @data_type
  33.  
  34.             fetch next from cur_field_list into @field_id
  35.  
  36.             set @first_field = 0
  37.         end
  38.  
  39.     set @query_text = @query_text + ' ) '
  40.  
  41.     close cur_field_list
  42.  
  43.     deallocate cur_field_list    
  44.  
  45.     EXEC (@query_text)
  46.  
  47.     declare cur_source_data cursor for
  48.         select fieldID, rec_match_code, rec_value
  49.         from tbl_records
  50.         where tableID = @prm_tableID and langID = @prm_langID
  51.         order by rec_match_code, fieldID
  52.  
  53.     open cur_source_data
  54.     --print @@cursor_rows
  55.  
  56.     set @active_match_code = ''
  57.  
  58.     fetch next from cur_source_data
  59.         into @field_id, @match_code, @value
  60.  
  61.     while @@fetch_status = 0 
  62.         begin
  63.             if @active_match_code <> @match_code
  64.                 begin
  65.                     set @query_text = 'insert into ' + @table_identity + ' (rec_match_code) values ( ' + '''' + @match_code + '''' + ' ) '
  66.  
  67.                     EXEC (@query_text)
  68.  
  69.  
  70.                     set @active_match_code = @match_code                
  71.                 end
  72.  
  73.             select @field_name = field_name, @data_type = field_datatype from tbl_fields where fieldID = @field_id
  74.  
  75.             set @query_text = 'update ' + @table_identity + ' set ' + @field_name + ' = ' + 'convert(' + @data_type + ', ' + '''' + @value + '''' + ') ' + ' where rec_match_code = ' + '''' + @match_code + ''''
  76.  
  77.             EXEC (@query_text)
  78.  
  79.             fetch next from cur_source_data
  80.                 into @field_id, @match_code, @value
  81.         end
  82.     close cur_source_data
  83.  
  84.     deallocate cur_source_data
  85.  
  86.  
  87.     EXEC('select * from ' + @table_identity)
  88.  
  89.     set @query_text = 'if exists (select * from dbo.sysobjects where name like ' + '''' + '%' + @table_identity + '%' + '''' + ') drop table ' + @table_identity
  90.  
  91.     exec(@query_text) 
  92.  
  93.  
  94.  
  95. end
  96. GO
  97.  
Nov 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6233
by: deprins | last post by:
Hello, I have wrote a stored procedure but its real slow. Its activated by a button on web page but its takes to long to process and the web server gives a timeout message after 5 minutes. Is there anyway to speed up this stored procedure? What am I doing wrong here? ------------------------------------------------------------------------------
10
3745
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty well and used a query plan that made sense. Now, I know what you're all thinking... stored procedures have to optimize for variable parameters, etc. Here's what I've tried to fix the issue: 1. Recompiled the stored procedure 2. Created a new,...
1
6481
by: Matt | last post by:
I would like to convert a couple informix stored procedures to SQL Server stored procedures. I have no idea how to accomplish this. Here is an example of one of the procedures I need to convert. drop function mnaf_calc_calendar_quarter; CREATE FUNCTION mnaf_calc_calendar_quarter(pEndDate Date) --***************************************************************************** -- Name: mnaf_calc_calendar_quarter -- Description/Notes:
6
2361
by: SandySears | last post by:
I am trying to use a stored procedure to insert a record using VS 2005, VB and SQL Server Express. The code runs without errors or exceptions, and returns the new identifer in the output parameters. It returns my error text message in another output parameter as "ok", which is the value that is set in the stored procedure prior to doing the insert. It returns my var for @@rowcount as 1. However, the record does not get into the table. ...
5
16559
by: Wael | last post by:
Hi, I have the following stored procedure that does some processing and puts the result in a temporary table. I tried several things that procedure to display output that I can access with ADO.Net, but it doesn't work. It doesn't even display the result in the query analyzer unless I add SELECT @ReturnFullName Any help? The stored procedure:
4
12978
by: Henrik Juul | last post by:
How do I call my Stored Procedure recursively: CREATE PROCEDURE dbo.GetParentIONode ( @IONodeID int, @FullNodeAddress char(100) OUTPUT ) AS BEGIN
6
5094
by: yin_n_yang74 | last post by:
I am trying to create a report in Crystal Reports (v 8.5). I have a stored procedure to pull data from two databases and parameters. There are multiple one-to-many relationships and the stored procedure returns duplicates; e.g., one schedule may have multiple resources, supplies, and/or orders (and one order may have multiple foods). Is there a way to stop the duplication? The stored procedure looks like this: ...
4
5682
by: Alchemist | last post by:
I am using Python 2.4 and Postgresql 8.2 database server. On the database I have created a stored function, example, CREATE OR REPLACE FUNCTION calculateaverage() I created a new python script and would like to call my database stored function. How can I call a database stored function/procedure in python?
1
3874
by: anoopmadavoor | last post by:
Hello, Is it possible to call a python function from a postgresql stored procedure,pls give me an example
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.